球衣基础教程

发布于 2024-12-01 17:13:35 字数 5421 浏览 1 评论 0原文

我正在使用 Jersey 1.8,我提到的教程已经很旧了。什么都不起作用。我指的是此处给出的教程

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

Hello.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 如下

<?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 我尝试运行时遇到的错误是

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.access$700(WebApplicationImpl.java:169)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.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)

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

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.

Hello.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

<?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.access$700(WebApplicationImpl.java:169)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-12-08 17:13:35

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

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

以下初始化参数是错误的

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

它应该引用包含服务的包。将您的服务放入包中并在 init 参数值中指定。无论是否是 Jersey,使用默认包始终是一种不好的做法。

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

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.

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.

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

黑色毁心梦 2024-12-08 17:13:35

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

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

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

然后访问 http://localhost:8080/basic-jersey /休息/测试

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文