HTTP状态500-当我添加@PUT方法时,servlet jersey-servlet的Servlet.init()抛出异常 [英] HTTP Status 500 - Servlet.init() for servlet jersey-servlet threw exception When I add @PUT method

查看:194
本文介绍了HTTP状态500-当我添加@PUT方法时,servlet jersey-servlet的Servlet.init()抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey来开发Rest API.我需要添加一个@PUT方法,但是会引发异常.

I am working on a rest api using Jersey. I need to add a @PUT method but it throws an exception.

这是我现在要添加的方法:

This is the method I am trying to add right now:

@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";
    }
...
}

实际上,该方法中包含更多内容,但是内容并不重要.当该方法存在于类中时,尝试访问服务器时出现以下异常:

In reality there is more content in the method but the content does not matter. When this method exists in the class, I get the following exception when trying to access the server:

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方法可以正常工作,但是当我添加这个新方法时,所有方法都会停止工作.

There are also other @PUT methods in the class that work as they should but when I add this new one, everything stops working.

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

This is the content of my web.xml file:

<!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?

If I change the @PUT annotation to @GET and remove the @Consumes annotation, everything works. Why can I not add a new @PUT?

推荐答案

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

Note that you are sending the LoginUser as json request body, so you need to remove @QueryParam("user").

这篇关于HTTP状态500-当我添加@PUT方法时,servlet jersey-servlet的Servlet.init()抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆