Jersey项目调用接口报HTTP ERROR 500错误求助
问题描述
用IntelliJ开发Jersey项目,配置了web.xml和继承ResourceConfig的MainClass(控制器与主类同包),执行./gradlew appRun启动项目后,调用http://localhost:8080/myservice出现HTTP 500错误,错误栈根因为java.lang.IllegalStateException: InjectionManagerFactory not found。
相关配置代码
web.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- See: http://docs.oracle.com/cd/E28613_01/web.1211/b65890/web_xml.htm --> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>JerseyApp</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>path.to.package.MainClass</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>JerseyApp</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
主类代码
@ApplicationPath("/") public class MainClass extends ResourceConfig { public MainClass() { // 扫描当前包下的组件 packages(this.getClass().getPackage().getName()); } }
控制器代码(与主类同包)
@Path("/abc") public class LabelController { @GET @Produces(MediaType.APPLICATION_JSON) public List<ABC> getResources(@QueryParam("something") List<String> ids) { // 方法实现逻辑 } }
build.gradle配置
buildscript { ext { LOMBOK_VERSION = "1.18.22" JAVAX_WSRS_VERSION = "2.1.1" } } plugins { id "org.gretty" version "4.0.3" } apply plugin: 'java-library' apply plugin: 'java' apply plugin: 'war' repositories { mavenLocal() maven { another intern repositories } } dependencies { compileOnly "org.projectlombok:lombok:${LOMBOK_VERSION}" testImplementation "org.projectlombok:lombok:${LOMBOK_VERSION}" annotationProcessor "org.projectlombok:lombok:${LOMBOK_VERSION}" implementation "com.microsoft.sqlserver:mssql-jdbc:9.4.1.jre11" implementation "javax.ws.rs:javax.ws.rs-api:${JAVAX_WSRS_VERSION}" implementation 'org.glassfish.jersey.core:jersey-server:3.0.8' implementation 'org.glassfish.jersey.containers:jersey-container-servlet:3.0.8' }
错误信息
HTTP ERROR 500 jakarta.servlet.ServletException: JerseyApp==org.glassfish.jersey.servlet.ServletContainer@80f88154{jsp=null,order=-1,inst=true,async=false,src=DESCRIPTOR:file:///C:/Projects/JerseyApp/build/inplaceWebapp/WEB-INF/web.xml,STARTED} URI: /myservice/ STATUS: 500 MESSAGE: jakarta.servlet.ServletException: JerseyApp==org.glassfish.jersey.servlet.ServletContainer@80f88154{jsp=null,order=-1,inst=true,async=false,src=DESCRIPTOR:file:///C:/Projects/JerseyApp/build/inplaceWebapp/WEB-INF/web.xml,STARTED} SERVLET: JerseyApp CAUSED BY: jakarta.servlet.ServletException: JerseyApp==org.glassfish.jersey.servlet.ServletContainer@80f88154{jsp=null,order=-1,inst=true,async=false,src=DESCRIPTOR:file:///C:/Projects/JerseyApp/build/inplaceWebapp/WEB-INF/web.xml,STARTED} CAUSED BY: java.lang.IllegalStateException: InjectionManagerFactory not found. Caused by: jakarta.servlet.ServletException: JerseyApp==org.glassfish.jersey.servlet.ServletContainer@80f88154{jsp=null,order=-1,inst=true,async=false,src=DESCRIPTOR:file:///C:/Projects/JerseyApp/build/inplaceWebapp/WEB-INF/web.xml,STARTED} at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:651) at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:486) at org.eclipse.jetty.servlet.ServletHolder.prepare(ServletHolder.java:731) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:524) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:131) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:578) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:122) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:223) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1571) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:221) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1375) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:176) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:484) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1544) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:174) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1297) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:129) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:149) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:122) at org.eclipse.jetty.server.Server.handle(Server.java:562) at org.eclipse.jetty.server.HttpChannel.lambda$handle$0(HttpChannel.java:505) at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:762) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:497) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:282) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:319) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:100) at org.eclipse.jetty.io.SelectableChannelEndPoint$1.run(SelectableChannelEndPoint.java:53) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:894) at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1038) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: java.lang.IllegalStateException: InjectionManagerFactory not found. at org.glassfish.jersey.internal.inject.Injections.lambda$lookupInjectionManagerFactory$0(Injections.java:74) at java.base/java.util.Optional.orElseThrow(Optional.java:403) at org.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:74) at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:69) at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:261) at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:339) at jakarta.servlet.GenericServlet.init(GenericServlet.java:178) at org.eclipse.jetty.servlet.ServletHolder$Wrapper.init(ServletHolder.java:1305) at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:633) ... 29 more
解决方案
错误原因
- 命名空间冲突:Jersey 3.x已完全迁移到Jakarta EE规范(包名从
javax.*改为jakarta.*),但当前依赖同时包含旧的javax.ws.rs-api(2.1.1)和Jersey 3.0.8,导致API不一致。 - 缺少注入实现:Jersey 3.x依赖HK2作为注入容器,当前配置未添加
jersey-hk2模块,导致找不到InjectionManagerFactory。 - web.xml参数过时:web.xml中使用的
javax.ws.rs.Application参数在Jakarta EE环境下需替换为jakarta.ws.rs.Application。
修复步骤
- 更新依赖,移除旧的
javax.ws.rs-api,添加Jakarta EE对应API及Jersey HK2注入模块。 - 修改web.xml中的初始化参数名,适配Jakarta EE规范。
- 确保所有Jersey相关依赖版本统一。
修改后的代码
build.gradle
buildscript { ext { LOMBOK_VERSION = "1.18.22" JAKARTA_WSRS_VERSION = "3.1.0" JERSEY_VERSION = "3.0.8" } } plugins { id "org.gretty" version "4.0.3" } apply plugin: 'java-library' apply plugin: 'java' apply plugin: 'war' repositories { mavenLocal() maven { // 内部仓库配置 } } dependencies { compileOnly "org.projectlombok:lombok:${LOMBOK_VERSION}" testImplementation "org.projectlombok:lombok:${LOMBOK_VERSION}" annotationProcessor "org.projectlombok:lombok:${LOMBOK_VERSION}" implementation "com.microsoft.sqlserver:mssql-jdbc:9.4.1.jre11" // 替换为Jakarta EE的JAX-RS API implementation "jakarta.ws.rs:jakarta.ws.rs-api:${JAKARTA_WSRS_VERSION}" // Jersey核心依赖 implementation "org.glassfish.jersey.core:jersey-server:${JERSEY_VERSION}" implementation "org.glassfish.jersey.containers:jersey-container-servlet:${JERSEY_VERSION}" // 添加Jersey HK2注入实现 implementation "org.glassfish.jersey.inject:jersey-hk2:${JERSEY_VERSION}" }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0"> <servlet> <servlet-name>JerseyApp</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <!-- 改为Jakarta版本的参数名 --> <param-name>jakarta.ws.rs.Application</param-name> <param-value>path.to.package.MainClass</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>JerseyApp</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
验证
重新执行./gradlew appRun启动项目,调用http://localhost:8080/myservice/abc(对应控制器的@Path("/abc"))即可正常访问接口。
内容的提问来源于stack exchange,提问作者emoleumassi
相关产品推荐
相关产品推荐

