java 部署基于 Jersey 的 Restful 服务项目中的 HTTPstatus 500

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17102393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:59:14  来源:igfitidea点击:

HTTPstatus 500 in deploying Jersey based Restful service project

javaresttomcatjersey

提问by Akina91

I have recently started with Jersey following thistutorial. I have configured the services/package name in web.xml alright, Now the thing is I am getting an javax.servlet.ServletException: Servlet.init() Exception with the following stack trace below:

我最近按照教程开始使用 Jersey 。我已经在 web.xml 中配置了服务/包名称,现在问题是我得到了一个 javax.servlet.ServletException: Servlet.init() 异常,下面的堆栈跟踪如下:

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)
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:771)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)

Here is my Jes.java Class file:

这是我的 Jes.java 类文件:

package webb;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/Jes")
public class Jes {

      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
            return "Hello Jersey";
          }

      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String sayXMLHello() {
            return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
          }

          // This method is called if HTML is request
          @GET
          @Produces(MediaType.TEXT_HTML)
          public String sayHtmlHello() {
            return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
          }

    public static void main(String[] args) {
    }

}

And web.xml

和 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>webb</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>webb</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Here is my application setup:

这是我的应用程序设置:

Apache Tomcat 6.0
Jersey-core-1.8.jar
Jersey-bundle-1.8.jar
asm-3.1.jar
JRE7
Windows 64 bit

Apache Tomcat 6.0
Jersey-core-1.8.jar
Jersey-bundle-1.8.jar
asm-3.1.jar
JRE7
Windows 64 位

->I am not able to figure out what is causing this com.sun.jersey.spi.inject.Errors$ErrorMessagesException, answers related to similar problem are welcome.

->我无法弄清楚是什么导致了这个 com.sun.jersey.spi.inject.Errors$ErrorMessagesException,欢迎提供与类似问题相关的答案。

采纳答案by Ori Dar

Not sure it's your problem, every thing seems OK, but the following mapping appear twice:

不确定这是你的问题,一切似乎都很好,但以下映射出现了两次:

  @GET
  @Produces(MediaType.TEXT_PLAIN)

Your sayXMLHello()should be annotated with @Produces(MediaType.TEXT_XML)instead

sayXMLHello()应该用@Produces(MediaType.TEXT_XML)而不是注释