java 泽西岛基础教程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7205761/
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-10-30 19:04:04  来源:igfitidea点击:

Jersey Tutorials from Basics

javarestjakarta-eejerseyjax-rs

提问by Shiv Kumar Ganesh

I am using Jersey 1.8 and the tutorials that I am referring are quiet old. Nothing works. I am referring to the tutorial given here

我使用的是 Jersey 1.8 并且我所指的教程很旧。没有任何作用。我指的是这里给出的教程

And get a class not found exception. as per the tutorial I made my Java Class as well as configured my web.xml. It shows me an exception and I am not getting a way to fix this. I would like to have a complete up to date tutorial for Jersey implementation. And if something is better than Jersey for REST implementation please suggest. I have rescently started with REST based web services and would appreciate if you can suggest me where to start from(I am only interested in REST). Below is the code that I wrote and compiled using eclipse.

并得到一个找不到类的异常。根据教程,我制作了我的 Java 类并配置了我的 web.xml。它显示了一个例外,我没有办法解决这个问题。我想要一个关于泽西岛实施的完整的最新教程。如果在 REST 实现方面有什么比 Jersey 更好的东西,请提出建议。我最近开始使用基于 REST 的 Web 服务,如果您能建议我从哪里开始,我将不胜感激(我只对 REST 感兴趣)。下面是我使用eclipse编写和编译的代码。

Hello.java

你好.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {

    //This method prints the Plain Text
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello()
    {
        return "Hello Jersey";
    }

    //This is the XML request output

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

    //This result is produced if HTML is requested

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHTMLHello()
    {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
    }

}

web.xml is as follows

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>RESTFullApp</display-name>
  <servlet>
  <servlet-name>JerseyRESTService</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>RESTFullApp</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>JerseyRESTService</servlet-name>
  <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
<!--  <welcome-file-list>-->
<!--    <welcome-file>index.html</welcome-file>-->
<!--    <welcome-file>index.htm</welcome-file>-->
<!--    <welcome-file>index.jsp</welcome-file>-->
<!--    <welcome-file>default.html</welcome-file>-->
<!--    <welcome-file>default.htm</welcome-file>-->
<!--    <welcome-file>default.jsp</welcome-file>-->
<!--  </welcome-file-list>-->
</web-app>

Noe the error I get while I try to run is

不,我尝试运行时遇到的错误是

exception 

javax.servlet.ServletException: Servlet.init() for servlet JerseyRESTService 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.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
    com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
    com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)
    com.sun.jersey.server.impl.application.WebApplicationImpl.access0(WebApplicationImpl.java:169)
    com.sun.jersey.server.impl.application.WebApplicationImpl.f(WebApplicationImpl.java:775)
    com.sun.jersey.server.impl.application.WebApplicationImpl.f(WebApplicationImpl.java:771)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    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)

回答by BalusC

You're not exactly following the tutorial. You changed bits here and there without actually understanding what they represents. In this particular case, according to the exception,

您没有完全按照教程进行操作。你在这里和那里改变了位,而没有真正理解它们代表什么。在这种特殊情况下,根据例外情况,

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

com.sun.jersey.api.container.ContainerException:ResourceConfig 实例不包含任何根资源类。

the following init param is wrong

以下初始化参数是错误的

<init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>RESTFullApp</param-value>
</init-param>

It should refer to the package containing the services. Put your service in a package and specify that in the init param value. Jersey or not, using the default package is always a bad practice.

它应该是指包含服务的包。将您的服务放入一个包中并在 init 参数值中指定。泽西岛与否,使用默认包总是一种不好的做法。

As to the tutorials, why don't you just read Jersey's own documentation? Jersey Wikiand Jersey User Guide.

至于教程,你为什么不阅读 Jersey 自己的文档?Jersey WikiJersey 用户指南

回答by Ryan Stewart

Try the Jersey User Guide. It will pretty much always have the most up-to-date tutorial. As for the error, your web.xml declares that your resources classes will be in a package called "RESTFullApp". 1) That's a peculiar package name, and 2) I don't see a package declaration on your class.

试试泽西岛用户指南。它几乎总是有最新的教程。至于错误,您的 web.xml 声明您的资源类将位于名为“RESTFullApp”的包中。1) 这是一个特殊的包名,并且 2) 我在你的班级上没有看到包声明。

Edit:For a working example, you check out my sample project on github. If you have git and maven, you can clone it and run it with

编辑:对于一个工作示例,您可以在 github 上查看我的示例项目。如果你有 git 和 maven,你可以克隆它并运行它

git clone git://github.com/zzantozz/testbed.git tmp
cd tmp
mvn jetty:run -pl basic-jersey

Then visit http://localhost:8080/basic-jersey/rest/test

然后访问http://localhost:8080/basic-jersey/rest/test