我尝试使用Jersey Jetty和Spring来设置一个简单的REST应用程序,为自动连接设置beans。我也在通过Gradle管理这件事。
我有两个主要endpoint< code>/users和< code>/greeting。
/hello
上的GET
仅返回文本响应并正常工作。那里没有自动安装的依赖项。
然而,如果我在< code>/usersendpoint上执行get,我将使用一个API类返回响应,该类应该由spring注入。该部分失败,因此每当我调用该endpoint时,都会得到一个< code > NullPointerException 。
还有一点需要注意的是,我想用的是球衣1.12和spring 4.2.0。原因是,这将是一个原型,我将不得不在其他地方插入。
执行GET on /users时出现异常:
java.lang.NullPointerException
at com.hateos.spring.test01.resources.user.controller.UserController.getAllUsers(UserController.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:126)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:591)
at java.lang.Thread.run(Thread.java:748)
19-03-20 15:10:59:694 WARN qtp1567581361-16 server.HttpChannel:517 - /users
java.lang.NullPointerException
at com.hateos.spring.test01.resources.user.controller.UserController.getAllUsers(UserController.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:126)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:591)
at java.lang.Thread.run(Thread.java:748)
项目结构如下(跳过不相关的文件):
hateos-spring-test01
|- src
| |- main
| | |- java
| | | |- com.hateos.spring.test01 (nested folders)
| | | |- resources
| | | | |- greeting
| | | | |- user
| | | | |- api
| | | | | |- impl
| | | | | | |- UserAPIImpl.java
| | | | | |- UserAPI.java
| | | | |- controller
| | | | |- UserController.java
| | | | |- exception
| | | | |- model
| | | | |- User.java
| | | | |- UserList.java
| | | |- MyApplication.java
| | |- resources
| | |- hateos-spring-test01.xml
| | |- log4j.properties
| |- test
|- build.gradle
|- gradlew
|- gradlew.bat
|- settings.gradle
build.gradle:
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = 'com.hateos.spring.test01.MyApplication'
ext {
jettyVersion = '9.4.6.v20170531'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
}
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'org.slf4j:slf4j-log4j12:1.6.6'
compile "org.codehaus.jackson:jackson-core-asl:1.9.2"
compile "org.codehaus.jackson:jackson-jaxrs:1.9.2"
compile "org.codehaus.jackson:jackson-mapper-asl:1.9.2"
compile "org.codehaus.jackson:jackson-xc:1.9.2"
compile "com.sun.jersey:jersey-client:1.12"
compile "com.sun.jersey:jersey-core:1.12"
compile "com.sun.jersey:jersey-json:1.12"
compile "com.sun.jersey:jersey-server:1.12"
compile "com.sun.jersey:jersey-servlet:1.12"
compile "org.codehaus.jettison:jettison:1.1"
compile "org.eclipse.jetty:jetty-server:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlet:${jettyVersion}"
compile 'org.springframework:spring-core:4.2.0.RELEASE'
compile 'org.springframework:spring-web:4.2.0.RELEASE'
compile 'org.springframework.hateoas:spring-hateoas:0.25.1.RELEASE'
compile 'org.springframework:spring-beans:4.2.0.RELEASE'
compile 'org.springframework:spring-context:4.2.0.RELEASE'
compile 'org.springframework:spring-context-support:4.2.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
spring xml文件(hateos-spring-text.xml):
<?xml version="1.0" encoding="UTF-8"?>
<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/>
<context:component-scan base-package="com.hateos.spring.test01"/>
<bean id="usersAPI" class="com.hateos.spring.test01.resources.user.api.impl.UserAPIImpl"/>
<bean id="usersController" class="com.hateos.spring.test01.resources.user.controller.UserController" />
</beans>
MyApplication.java
package com.hateos.spring.test01;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyApplication {
private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
logger.info("Hi guys!");
Server server = new Server(9090);
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);
ServletHolder servletHolder = servletContextHandler.addServlet(ServletContainer.class, "/*");
servletHolder.setInitOrder(0);
servletHolder.setInitParameter(
"com.sun.jersey.config.property.packages",
"com.hateos.spring.test01.resources"
);
try {
server.start();
server.join();
} catch (Exception ex) {
logger.error("Error occurred while starting Jetty", ex);
System.exit(1);
} finally {
server.destroy();
}
}
}
/资源/用户/api/UserAPI.java
package com.hateos.spring.test01.resources.user.api;
import com.hateos.spring.test01.resources.user.model.User;
import com.hateos.spring.test01.resources.user.model.UserList;
public interface UserAPI {
UserList getAll();
User getById(String userId);
}
/resources/user/api/impl/UserAPI.java
package com.hateos.spring.test01.resources.user.api.impl;
import com.hateos.spring.test01.resources.user.api.UserAPI;
import com.hateos.spring.test01.resources.user.model.User;
import com.hateos.spring.test01.resources.user.model.UserList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class UserAPIImpl implements UserAPI {
private static final Logger logger = LoggerFactory.getLogger(UserAPIImpl.class);
@Override
public UserList getAll() {
UserList userList = new UserList();
for (int index = 0; index < userIds.length; index++) {
userList.getUserList().add(toUser(index));
}
return userList;
}
@Override
public User getById(String userId) {
return findUserById(userId);
}
private User toUser(int index) {
User newUser = null;
if (index < userIds.length) {
newUser = new User();
newUser.setFirstName(firstNames[index]);
newUser.setLastName(lastNames[index]);
newUser.setUserId(userIds[index]);
}
return newUser;
}
private User findUserById(String userId) {
for (int index = 0; index < userIds.length; index++) {
if (userIds[index].equals(userId)) {
return toUser(index);
}
}
return null;
}
private static String[] firstNames = {"Bob", "Jerry", "Milton", "Carrie"};
private static String[] lastNames = {"Builder", "Seinfeld", "Bombay", "Fisher"};
private static String[] userIds = {"001", "153", "5566", "234"};
}
/resources/user/controller/user controller . Java
package com.hateos.spring.test01.resources.user.controller;
import com.hateos.spring.test01.resources.user.api.UserAPI;
import com.hateos.spring.test01.resources.user.exception.UserNotFoundException;
import com.hateos.spring.test01.resources.user.model.User;
import com.hateos.spring.test01.resources.user.model.UserList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/users")
@Component
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
private UserAPI userAPI;
@Autowired
public void setUserAPI(UserAPI usersAPI) {
logger.info("userAPI is being injected");
this.userAPI = usersAPI;
}
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public UserList getAllUsers() {
logger.info("getAllUsers is being called");
return this.userAPI.getAll();
}
@GET
@Path("/{uid}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public User getUser(@PathParam("uid") String userId) {
User foundUser = userAPI.getById(userId);
if (foundUser != null) {
return foundUser;
} else {
throw new UserNotFoundException(userId);
}
}
}
编辑1:启动时的控制台日志
5:06:41 PM: Executing task 'run'...
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :run
19-03-20 17:06:42:452 INFO main test01.MyApplication:15 - Hi guys!
19-03-20 17:06:42:496 INFO main util.log:192 - Logging initialized @446ms to org.eclipse.jetty.util.log.Slf4jLog
19-03-20 17:06:42:618 INFO main server.Server:372 - jetty-9.4.6.v20170531
Mar 20, 2019 5:06:42 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.hateos.spring.test01.resources
Mar 20, 2019 5:06:42 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class com.hateos.spring.test01.resources.greeting.controller.GreetingController
class com.hateos.spring.test01.resources.user.controller.UserController
class com.hateos.spring.test01.resources.shared.RootController
Mar 20, 2019 5:06:42 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
class com.hateos.spring.test01.resources.shared.controller.BaseExceptionMapper
Mar 20, 2019 5:06:42 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM'
19-03-20 17:06:43:540 INFO main handler.ContextHandler:788 - Started o.e.j.s.ServletContextHandler@4b29d1d2{/,null,AVAILABLE}
19-03-20 17:06:44:316 INFO main server.AbstractConnector:280 - Started ServerConnector@4a003cbe{HTTP/1.1,[http/1.1]}{0.0.0.0:9090}
19-03-20 17:06:44:316 INFO main server.Server:444 - Started @2270ms
编辑2:此问题与链接的副本不同。我使用球衣,他们没有。我的解决方案(由LppEdd给出)是通过初始主函数让球衣Spring彼此配合良好。我的问题是关于基于xml的Spring豆自动布线与泽西岛。
您很可能需要指定与UserAPI
关联的属性
<bean id="usersController" class="com.hateos.spring.test01.resources.user.controller.UserController" />
<property name="userAPI" ref="usersAPI" />
</bean>
但是,说实话,我会选择构造函数注入。干净多了。
@Path("/users")
@Component
class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
private final UserAPI userApi;
UserController(final UserAPI userApi) {
this.userApi = userApi;
}
...
}
用
<bean id="usersController" class="com.hateos.spring.test01.resources.user.controller.UserController" />
<constructor-arg ref="usersAPI" />
</bean>
尽管您已经使用
<context:annotation-config/>
<context:component-scan base-package="com.hateos.spring.test01"/>
所以
请参阅注释。
// You forgot about this!
servletContextHandler.addEventListener(new ContextLoaderListener());
servletContextHandler.setInitParameter("contextConfigLocation", "classpath*:**/hateos-spring-text.xml");
// You basically already have this part. Added for clarity
final ServletHolder jerseyServlet = new ServletHolder(SpringServlet.class);
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter(
"com.sun.jersey.config.property.packages",
"com.hateos.spring.test01"
);
servletContextHandler.addServlet(jerseyServlet, "/*");
应该足够了。
我正在尝试使用Spring3.2.1创建spring-mvc测试。根据一些教程,我认为这将是直接的。 下面是我的测试: 下面是我的相关pom.xml: 我有以下测试配置类: 我的理解是,添加将强制Spring注入它。但是当我从Eclipse中运行这个测试时,我得到: 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException
我使用RoutingDataSource为应用程序的每个租户动态创建数据源。8-12小时后,应用程序与数据库失去连接,我得到了jpa事务异常。我发现以下属性负责验证和维持数据库连接,所以我将它们放在了应用程序中。财产。 数据源bean是在下面的类中创建的。如何将上述属性注入每个目标数据源?
我有以下课程: 我从控制器调用方法test(): 然而,spring并没有注入构建。版本值,即使类具有组件注释。 我在控制器中使用相同的属性,它工作正常。 我做错了什么?
我是一名学生,我对symfony和stackoverflow非常陌生,如果我犯了一些错误,请原谅。 以下是我尝试访问页面时出现的错误: [语义错误]方法AppBundle\Controller\FormController::newAction()中的注释“@Route”从未导入。您是否忘记为该注释添加“use”语句?在/home/buddy/bamber/realbaumber/src/AppB
问题内容: 我目前正在尝试将Dagger 2集成到Android应用程序中。我的项目设置如下: 图书馆 应用(取决于库) 在我的库项目中,我定义了一个类,稍后将其插入到库以及应用程序项目中的其他需要该类的类(活动和常规类)中。 现在-例如在我的“片段”或“活动”或常规类中,我将上述Singleton注入如下: 大概我想,因为实际上myManager始终为null。显然,它的构造函数也从未被调用过,
我有一个Spring启动应用程序,我最近从v1.3.3. RELEASE升级到v1.4.2. RELEASE。 用于我在v1中的集成测试。3.3,我有一个豆子,我能够成功地监视它。我在运行测试时,配置文件,下面的被激活,而不是应用程序的。 我正在升级到v1。4.2.发布并希望使用spyBean注释模拟单个方法,而不依赖于概要文件。 我对我的测试方法做了以下改变,以尝试它- 然而,当我尝试上述方法时