Jersey1.x + Spring 依賴注入出現 空指針異常

在使用rest風格和Spring的依賴注入作一個demo的時候。依賴注入 出現空指針異常html

 The RuntimeException could not be mapped to a response, re-throwing to the HTTP container java.lang.NullPointerException

   就是沒有 注入過去。。。java

網上查了查。終於找都一種辦法了。web

http://www.cnblogs.com/qunyang/p/3328561.htmlspring


 引用博主的原文:app

<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>

這裏有一點須要注意maven

  • Jersey的servlet-class一項,必定是上面所示的:ide

    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>

    請不要換成其它的類,以避免發生bean不能正常注入的問題this


可是我使用的是 REST服務類型的另一種,就是 存在Application 子類,不存在ApplicationPath註解。。個人下面寫的這種辦法我仍是沒有解決。你們會的幫個忙url

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;
	}
}



//spa

爲了上傳下載。該到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 空指針異常的!!!!   

相關文章
相關標籤/搜索