Autowired 为 null 且无法使用 Jersey + Spring
Autowired is null and not working with Jersey + Spring
我无法弄清楚为什么我的 Jersey RESTful 入口点找不到我在应用程序服务器启动时配置的 Spring Bean。从
尝试后,它一直收到 NullPointerException
- Spring DI - Autowired property is null in a REST service
- NullPointerException on @Autowired attribute with Jersey and Spring for REST service
- @Autowired is not working with jersey and spring
- Integrating both spring mvc and Jersey, getting a null pointer when viewing a jersey endpoint
- Jersey 2 + Spring: @Autowired is null
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 Mapper(这有一个XML与之关联)
public interface UserSql
{
List<Item> getAllItemsFor(@Param("userId") String userId);
}
我也从org.glassfish.jersey
改成了com.sun.jersey
,但是没用。我 运行 不知道哪里出了问题。谁能看出我做错了什么?
I provided for your previous question 有 link 四个 完整 工作示例。您可以轻松地抓住其中一个示例并在其基础上进行构建。
我将向您介绍 一个 示例。也许这太难了。我将使用 Jersey 2.x with Spring XML config.
首先,确保你有the dependencies(只显示未在 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
添加到项目class-path.
第四,MyApplication
class列在前面web.xml.
如果您按照示例进行操作,您将获得一个有效的示例。它可能不是您想要配置 Spring 组件的确切方式,但您将拥有一个工作示例,您可以基于该示例构建并进行调整以查看哪些有效,哪些无效。当你感觉更舒服时,你可以看到其他配置的其他示例(在这个答案的第一个 link 中)styles/options.
我用 SpringBeanAutowiringSupport 扩展了 ServiceImpl 和 DAOImpl 类,它解决了我的自动装配空指针异常。
在这里您可以创建该服务的实例并访问该服务的方法。我认为这是最简单的方法。
UserService user = new UserService();
user.saveUser();
我无法弄清楚为什么我的 Jersey RESTful 入口点找不到我在应用程序服务器启动时配置的 Spring Bean。从
尝试后,它一直收到 NullPointerException- Spring DI - Autowired property is null in a REST service
- NullPointerException on @Autowired attribute with Jersey and Spring for REST service
- @Autowired is not working with jersey and spring
- Integrating both spring mvc and Jersey, getting a null pointer when viewing a jersey endpoint
- Jersey 2 + Spring: @Autowired is null
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 Mapper(这有一个XML与之关联)
public interface UserSql
{
List<Item> getAllItemsFor(@Param("userId") String userId);
}
我也从org.glassfish.jersey
改成了com.sun.jersey
,但是没用。我 运行 不知道哪里出了问题。谁能看出我做错了什么?
我将向您介绍 一个 示例。也许这太难了。我将使用 Jersey 2.x with Spring XML config.
首先,确保你有the dependencies(只显示未在 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
添加到项目class-path.
第四,MyApplication
class列在前面web.xml.
如果您按照示例进行操作,您将获得一个有效的示例。它可能不是您想要配置 Spring 组件的确切方式,但您将拥有一个工作示例,您可以基于该示例构建并进行调整以查看哪些有效,哪些无效。当你感觉更舒服时,你可以看到其他配置的其他示例(在这个答案的第一个 link 中)styles/options.
我用 SpringBeanAutowiringSupport 扩展了 ServiceImpl 和 DAOImpl 类,它解决了我的自动装配空指针异常。
在这里您可以创建该服务的实例并访问该服务的方法。我认为这是最简单的方法。
UserService user = new UserService();
user.saveUser();