使用Jersey-Spring麻烦在Jersey上自动装配Spring依赖关系 [英] Trouble Autowiring Spring Dependencies In Jersey Using Jersey-Spring

查看:57
本文介绍了使用Jersey-Spring麻烦在Jersey上自动装配Spring依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jersey-Spring在我的Jersey应用程序中进行DI设置.但是,当我碰到一条路线时,出现500个错误,并看到下面的堆栈跟踪:

I'm attempting to get DI setup in my Jersey application using Jersey-Spring. However when I hit a route I'm getting 500 errors and see the stack trace below:

No beans found. Resolution failed for type class com.roommateAPI.service.HelloWorldService.
/RoommateAPI/hello/two
java.lang.NullPointerException
    at com.roommateAPI.resources.HelloWorldResource.helloWorldTwo(HelloWorldResource.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:406)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:350)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
> Building > :jettyRun > Running at http://localhost:8080/RoommateAPI

从SO研究来看,我似乎没有加载Spring Config,但我不知道为什么.我正在使用基于Java的配置.

From the SO research I've done it appears my Spring Config isn't being loaded, but I don't know why. I'm using the Java based configuration.

我的豆子

package com.roommateAPI.service;

import org.springframework.stereotype.Component;

@Component
public class HelloWorldService {

    public String sayHelloTwo() {
        return "Hello World 2!";
    }
}

我的资源

package com.roommateAPI.resources;

import com.roommateAPI.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("hello")
public class HelloWorldResource {

    @Autowired
    HelloWorldService helloWorldService;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String helloWorld() {
        return "Hello world!";
    }

    //This is an example test using DI/mocking
    @GET
    @Path("/two")
    @Produces(MediaType.TEXT_PLAIN)
    public String helloWorldTwo() {
        return helloWorldService.sayHelloTwo();
    }
}

我的Java配置

package com.roommateAPI.config;

import com.roommateAPI.service.HelloWorldService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.roommateAPI.service")
public class SpringApplication {
    @Bean(name="hellowWorldService")
    public HelloWorldService helloWorldService() {
        return new HelloWorldService();
    }
}

Jersey Config

package com.roommateAPI.config;

import com.roommateAPI.service.HelloWorldService;
import org.glassfish.jersey.server.ResourceConfig;

public class JerseyApplication extends ResourceConfig {

    public JerseyApplication() {
        register(HelloWorldService.class);
    }
}

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>Roommate API</display-name>

    <module-name>roommateapi</module-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfiguration</param-name>
        <param-value>com.roommateAPI.config.SpringApplication</param-value>
    </context-param>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <servlet>
        <servlet-name>JerseyServlet</servlet-name>                        co
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.roommateAPI.config.JerseyApplication</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.roommateAPI.resources</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>JerseyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

依赖项

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.5'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.6'
    compile 'org.glassfish.jersey.media:jersey-media-moxy:2.4.1'
    compile 'org.glassfish.jersey.ext:jersey-spring3:2.4'
    testCompile 'org.glassfish.jersey.test-framework:jersey-test-framework-core:2.9.1'
    testCompile 'org.mockito:mockito-all:1.9.5'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

我尝试过的事情:

每个我已经确保我有Spring ContextLoaderListener设置为弹簧加载.

Per this I've ensured I have the Spring ContextLoaderListener set so Spring loads.

推荐答案

我最终发现我无法通过web.xml连接Java配置.因此,我使用以下代码向我的WEB-INF目录中添加了一个applicationContext.xml:

I finally discovered that I cannot wire up Java Configuration via the web.xml. So I added an applicationContext.xml to my WEB-INF directory with the code below:

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd"
        >

    <context:annotation-config/>
        <bean class="com.roommateAPI.config.SpringApplication"/>
        <!--<bean id="HelloWorldService" class="com.roommateAPI.service.HelloWorldService" />-->
    </beans>

然后,我遇到另一个问题,在我的SpringApplication.java中,我有自动装配和@Bean声明,而Spring无法弄清楚我想要哪个.所以我的JavaApplication现在看起来像这样:

And then I had another issue where in my SpringApplication.java I had Autowiring and a @Bean declaration and Spring couldn't figure out which I wanted. So my JavaApplication now looks like this:

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>Roommate API</display-name>

    <module-name>roommateapi</module-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfiguration</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>JerseyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.roommateAPI.config.JerseyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>JerseyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

SpringApplication.java

package com.roommateAPI.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.roommateAPI.service")
public class SpringApplication {

}

这篇关于使用Jersey-Spring麻烦在Jersey上自动装配Spring依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆