Autowired为空,不适用于Jersey + Spring

彼得

我在弄清楚为什么我的Jersey RESTful入口点找不到应用服务器启动时配置的Spring Bean时遇到问题。尝试从后一直获取NullPointerException

Web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.testing.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Spring-context.xml

<context:annotation-config />
<context:component-scan base-package="com.testing.config, com.testing.repository, com.testing.workflow" />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>

泽西servlet入口点

@Component
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}/items")
public class UserResource
{
  @Autowired
  private UserWorkFlow userWorkFlow;

  @GET
  public Response getAllItems(@PathParam("userId") String userId)
  {
    // ** NullPointerException here complains about the UserWorkFlow 
    return Response.status(200).entity(userWorkFlow.getItemsFor(userId)).build();
  }
}

服务层

我也尝试为此创建一个接口,但是没有用。

@Service
public class UserWorkFlow
{
  @Autowired
  private AllItems allItems;

  public List<Item> getItemsFor(String id)
  {
    return allItems.getItemsFor(id);
  }
}

仓库和DAO

@Repository
public class AllItems
{
  @Autowired
  private ItemSql itemSql;

  public List<Item> getItemsFor(String id)
  {
    return itemSql.getAllItemsFor(id);
  }
}

MyBatis映射器(具有与之关联的XML)

public interface UserSql
{
  List<Item> getAllItemsFor(@Param("userId") String userId);
} 

我也com.sun.jersey改为,org.glassfish.jersey但没有用。我没什么主意了。谁能发现我做错了什么?

保罗·萨姆索塔

我为您先前的问题提供链接具有指向四个完全正常工作的示例的链接您可以很容易地抓住其中一个示例并在其之上进行构建。

我将向您介绍其中一个示例。也许太难了。我将在Spring XML config中使用Jersey2.x

首先,确保您具有依赖项(仅显示pom中未显示的版本)

  • jersey-container-servlet: 2.22.1
  • spring-web: 3.2.3.RELEASE
  • commons-logging
  • jersey-spring3: 2.22.1(注意快照项目使用jersey-spring*4*。尚未发布,将在下一个Jersey版本中发布。)

其次,确保你web.xml的秩序良好

第三,将您添加applicationContext.xml到项目的类路径。

FouthMyApplication在前面的web.xml中列出类。

如果您将示例遵循到T,则将有一个有效的示例。也许这不是您想要配置Spring组件的确切方法,但是您将获得一个可行的示例,可以在此基础上进行构建和调整,以查看有效的方法和无效的方法。当您感到更舒适时,可以看到其他示例(在此答案的第一个链接中)以了解其他配置样式/选项。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章