Autowired为空并且与Jersey + Spring不兼容。

6

我遇到了一个问题,就是我的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>

Jersey 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 Mapper(这个有关联的XML文件)
public interface UserSql
{
  List<Item> getAllItemsFor(@Param("userId") String userId);
} 

我也尝试从org.glassfish.jersey改为com.sun.jersey,但没有起作用。我已经没有任何想法了,有人能发现我做错了什么吗?


你能提供日志错误信息吗? - Rana_S
你是如何将Spring与Jersey集成的? - Sotirios Delimanolis
为什么不将组件扫描放在根上下文中,以便所有servlet共享bean。而不是放在特定的servlet上。 <context:component-scan base-package="com.testing" /> - Rana_S
2个回答

4

我为你的之前问题提供的链接中有四个完全可用的示例。 你可以轻松地选择其中一个示例并在其上构建。

我将带你了解其中一个示例。也许那些示例太难跟进了。 我将使用Jersey 2.x with Spring XML config

首先,确保你有依赖项(仅显示在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 添加到项目类路径中。

第四点,MyApplication 类在之前的 web.xml 中列出。
如果您完全按照示例操作,您将得到一个可工作的示例。这可能不是您想要配置 Spring 组件的确切方式,但您将拥有一个可工作的示例,您可以在此基础上进行构建和调整,以查看哪些方法有效,哪些方法无效。当您变得更加熟悉时,您可以查看其他示例(在本回答的第一个链接中),了解其他配置样式/选项。

添加 jersey-spring5 对我来说很有用!(因为我正在使用Spring 5) - King Midas
请注意,除了指向之前的SO问题的链接外,此答案中的所有链接都会从GitHub返回404页面未找到。 - JRSofty

1
我已经使用SpringBeanAutowiringSupport扩展了ServiceImpl和DAOImpl类,这解决了我的自动装配空指针异常问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接