Jersey REST Service Error

来源:互联网 发布:有意境的网名知乎 编辑:程序博客网 时间:2024/06/05 17:20

當用Jersey時遇上  HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception, 有機會是出於 JAX-RS 1 and JAX-RS 2 jar 都在參考位置, 要在pom.xml 加一些排除。

錯誤類型:

HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception

type Exception report

message Servlet.init() for servlet Jersey REST Service threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exceptionorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:745)

root cause

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:331)org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)javax.servlet.GenericServlet.init(GenericServlet.java:158)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:745)


錯誤解決辦法:
(原文)

Note: Please see above comments for further discussion and tips.

This error usual means that you have a both a JAX-RS 1 and JAX-RS 2 jar on the classpath. Jersey 2 uses JAX-RS 2 (javax.ws.rs-api-2.0.1.jar), but if you have the jsr311-api.jar also, which is JAX-RS 1, there is a javax.ws.rs.core.Application in each jar. But the jsr311-api Application doesn't have the method getProperties() (hence NoSuchMethodError).

I've come to the conclusion that all you need to do is add the above exclusion to the swagger dependency. The Jackson 2.0 provider (which depends on JAX-RS 1) seems to be overridden by a 2.4.1 provider (which uses the new version). So we don't need to add it ourselves. When it's overridden, it seems to leave behind the jsr311-api.jar. So if we exclude it, no one can attempt to use it, which looks to be the current problem

<dependency>    <groupId>com.wordnik</groupId>    <artifactId>swagger-core_2.10</artifactId>    <version>1.3.11</version>    <exclusions>        <exclusion>            <groupId>javax.ws.rs</groupId>            <artifactId>jsr311-api</artifactId>        </exclusion>    </exclusions></dependency>
將上面整段加入pom.xml 的 Dependencies 內就可解決。
1 0