gpt4 book ai didi

java - 使用 Jersey-Spring 在 Jersey 中 Autowiring Spring 依赖项时出现问题

转载 作者:行者123 更新时间:2023-12-02 04:54:19 25 4
gpt4 key购买 nike

我正在尝试使用 Jersey-Spring 在我的 Jersey 应用程序中进行 DI 设置。然而,当我到达一条路线时,我收到了 500 个错误,并看到下面的堆栈跟踪:

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 的配置。

我的 bean

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

泽西配置

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

我尝试过的:

根据 this我已确保已设置 Spring ContextLoaderListener,以便加载 Spring。

最佳答案

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

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 中我有 Autowiring 和 @Bean 声明,而 Spring 无法弄清楚我想要哪个。所以我的 JavaApplication 现在看起来像这样:

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 {

}

关于java - 使用 Jersey-Spring 在 Jersey 中 Autowiring Spring 依赖项时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24436388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com