在使用rest风格和Spring的依赖注入做一个demo的时候。依赖注入 出现空指针异常
The RuntimeException could not be mapped to a response, re-throwing to the HTTP container java.lang.NullPointerException
就是没有 注入过去。。。
网上查了查。终于找都一种办法了。
http://www.cnblogs.com/qunyang/p/3328561.html
引用博主的原文:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: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.mkyong.rest</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>
这里有一点需要注意:
Jersey的servlet-class一项,一定是上面所示的:
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
请不要换成其它的类,以免发生bean不能正常注入的问题
但是我使用的是 REST服务类型的另外一种,就是 存在Application 子类,不存在ApplicationPath注解。。我的下面写的这种办法我还是没有解决。大家会的帮个忙
public class ConfigApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(ServiceConfig.class);
classes.add(ServerSerivce.class);
classes.add(QiniuService.class);
return classes;
}
}
//
为了上传下载。该到2.x 因为试验很多次jersey1.x都不成功的。。而2.x则可以。但是和spring注入又出现问题了。
对jersey2.x 以上我使用
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.12</version></dependency>
又是空异常的了。。。。
今天早上在 stack overflow 上找到了答案。。
地址:http://stackoverflow.com/questions/22417498/migrating-from-spring-jersey-1-x-to-2-x
When I upgraded from Spring-Jersey 1.x to 2.x, I had the exact same problem.
I found that I was still including the jersey-core-1.18.1 jar.
Removing it fixed this problem!
答案说的就是 你导入的项目有有jersey1.x 也有jersey2.x 的。。。。因为他们确实是不一样。。
之前确实是在maven中加入过jersey1.x 但是我之后都已经移除了。。。可能是在maven中的 .m/repository 里还存在jersey1.x so spring ioc 空指针异常的!!!!