Autowired 为空且不适用于 Jersey + Spring

pet*_*ter 5 java spring dependency-injection jersey mybatis

我无法弄清楚为什么 My Jersey RESTful 入口点找不到我在应用服务器启动时配置的 Spring Bean。尝试后,它不断收到 NullPointerException

网页.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>
Run Code Online (Sandbox Code Playgroud)

弹簧上下文.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>
Run Code Online (Sandbox Code Playgroud)

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

服务层

我也尝试为此制作一个界面,但没有用。

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

  public List<Item> getItemsFor(String id)
  {
    return allItems.getItemsFor(id);
  }
}
Run Code Online (Sandbox Code Playgroud)

存储库和 DAO

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

  public List<Item> getItemsFor(String id)
  {
    return itemSql.getAllItemsFor(id);
  }
}
Run Code Online (Sandbox Code Playgroud)

MyBatis Mapper(它有一个与之关联的 XML)

public interface UserSql
{
  List<Item> getAllItemsFor(@Param("userId") String userId);
} 
Run Code Online (Sandbox Code Playgroud)

我也改成com.sun.jerseyfromorg.glassfish.jersey但没用。我想不出什么可能是错的。谁能发现我做错了什么?

Pau*_*tha 4

我为您之前的问题提供的链接包含四个完整工作示例的链接。您可以轻松地抓住其中一个示例并在其基础上进行构建。

我将引导您完成其中一个示例。也许这太难遵循了。我将使用Jersey 2.x 和 Spring XML 配置

首先,确保您具有依赖项(仅显示 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 组件的确切方式,但是您将拥有一个可以使用的工作示例,您可以根据该示例进行构建和调整,以了解哪些有效,哪些无效。当您变得更舒服时,您可以查看其他配置样式/选项的其他示例(在此答案的第一个链接中)。