HTTP状态500 - servlet jersey-servlet的Servlet.init()引发异常当我添加@PUT方法

时间:2017-08-08 08:23:58

标签: java maven jersey put

我正在使用泽西岛休息api。我需要添加@PUT方法,但它会引发异常。

这是我现在尝试添加的方法:

@Path("/login")
public class LoginController {
...
    @PUT
    @Path("/user")
    @Consumes("application/json;charset=utf-8")
    @Produces("application/json;charset=utf-8")
    public String putUser(@QueryParam("user")LoginUser lu) {
        return "login failed";
    }
...
}

实际上,该方法中的内容更多,但内容无关紧要。当该方法存在于类中时,在尝试访问服务器时出现以下异常:

javax.servlet.ServletException: Servlet.init() for servlet jersey-servlet threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:748)
root cause

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:339)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)`enter code here`
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:748)

该类中还有其他@PUT方法可以正常工作,但是当我添加这个新方法时,一切都停止工作。

这是我的web.xml文件的内容:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>REST API</display-name>

  <servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>

</web-app>

如果我将@PUT注释更改为@GET并删除@Consumes注释,则一切正常。为什么我不能添加新的@PUT

1 个答案:

答案 0 :(得分:0)

请注意,您要将LoginUser作为json请求正文发送,因此您需要删除@QueryParam("user")