eclipse Jersey、Tomcat:请求的资源不可用错误

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

Jersey, Tomcat: The requested resource is not available error

eclipseresttomcat7jersey-2.0rad

提问by Jake Schuurmans

I have been working towards getting a RESTful service set up using Jersey and Tomcat in RAD 8.5. I have looked at tons of stackoverflow questions related to my error with none of them working. There are no errors in my console.

我一直致力于在 RAD 8.5 中使用 Jersey 和 Tomcat 来设置 RESTful 服务。我已经查看了大量与我的错误相关的 stackoverflow 问题,但没有一个起作用。我的控制台中没有错误。

When I just type: http://localhost:8080/, I get the Apache homepage, so the server is working, but http://localhost:8080/jersey/rest/helloor http://localhost:8080/jersey/WEB-INF/classes/jersey/Hello.javadoes not work.

当我输入:http://localhost:8080/ 时,我得到了 Apache 主页,所以服务器正在工作,但是http://localhost:8080/jersey/rest/hellohttp://localhost:8080/jersey /WEB-INF/classes/jersey/Hello.java不起作用。

Here is the error: (with my library of jars on the side) Here is my Hello.java

这是错误:(旁边有我的罐子库) 这是我的Hello.java

package jersey;

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 is called if TEXT_PLAIN is request
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
        return "Hello Jersey";
      }

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

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

And my web.xml

还有我的 web.xml

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.example</param-value>
    </init-param>
</servlet>

Versions:

版本:

  • Tomcat: 7.0.663
  • RAD: 8.5
  • Jersey: 2.19
  • 雄猫:7.0.663
  • 辐射:8.5
  • 球衣:2.19

Thanks,

谢谢,

In Response to MaciejThis worked! I needed to add <servlet-mapping>with url pattern of /*. Then use http://localhost:8080/jersey/hello, I got a response from the server!

回应 Maciej这有效!我需要添加<servlet-mapping>与URL模式/*。然后使用http://localhost:8080/jersey/hello,我得到了服务器的响应!

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="jersey" version="2.5">
    <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>jersey</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

采纳答案by Maciej Lach

You are deploying a compiled code to Tomcat, so you won't be able to access the *.java resources.

您正在将编译后的代码部署到 Tomcat,因此您将无法访问 *.java 资源。

Annotation @Path("/hello")indicates the path at which resource is available.

注释@Path("/hello")指示资源可用的路径。

It is set to: base URL + /your_path. The base URLis based on your application name, the servlet and the URL pattern from the web.xml:

它设置为:base URL + /your_path。这base URL是基于您的应用程序名称、servlet 和来自以下内容的 URL 模式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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="jersey" version="2.5">
    <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>jersey</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Also replace @Producesannotation to @Consumes:

还将@Produces注释替换为@Consumes

package jersey;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

      // This method is called if TEXT_PLAIN is request
      @GET
      @Consumes(MediaType.TEXT_PLAIN)
      public String sayPlainTextHello() {
        return "Hello Jersey";
      }

      // This method is called if XML is request
      @GET
      @Consumes(MediaType.TEXT_XML)
      public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
      }

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

Try: http://localhost:8080/jersey/hello

试试:http://localhost:8080/jersey/hello

回答by Towhidul Haque Roni

Make sure that you have kept all the required Jersey Jar files in "WEB-INF -> lib" folder

确保您已将所有必需的 Jersey Jar 文件保存在“WEB-INF -> lib”文件夹中

回答by Aishwarya Sai Chamanoor

Even after following the steps as mentioned by Maciej, if it says 404 resource not found, Mention the subclass that implements Application class and write it within init-param tag in web.xml

即使按照 Maciej 提到的步骤进行操作,如果显示 404 资源未找到,请提及实现 Application 类的子类并将其写入 web.xml 的 init-param 标记中

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>packagename.java_class_name</param-value> 
    </init-param>

This worked for me.

这对我有用。