我遇到了一个问题,就是我的Jersey RESTful入口无法找到我在应用程序服务器启动时配置的Spring Bean。尝试后仍然会出现NullPointerException。
- Spring DI - 自动装配属性在REST服务中为空
- 使用Jersey和Spring进行REST服务时@Autowired属性上出现NullPointerException
- @Autowired在jersey和spring中不起作用
- 集成Spring MVC和Jersey,查看Jersey端点时获取空指针异常
- Jersey 2 + Spring:@Autowired为空
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
,但没有起作用。我已经没有任何想法了,有人能发现我做错了什么吗?