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

Jersey, Tomcat: The requested resource is not available error

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

当我输入:http://localhost:8080/, I get the Apache homepage, so the server is working, but http://localhost:8080/jersey/rest/hello or http://localhost:8080/jersey/WEB-INF/classes/jersey/Hello.java 不起作用。

这是错误:(旁边有我的 jar 库) 这是我的 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> ";
      }
}

还有我的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>

版本:

谢谢,

回应 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>

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

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

设置为:base URL + /your_pathbase URL 基于您的应用程序名称、servlet 和 web.xml:

中的 URL 模式
<?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>

同时将 @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> ";
      }
}

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

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

即使按照 Maciej 提到的步骤进行操作后,如果它说 404 资源未找到,请提及实现应用程序 class 的子 class 并将其写入 [= 中的 init-param 标记中15=]

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

这对我有用。

这个问题通过这些步骤解决了

 1. Select all files in lib folder and right click on it

 2. Then click add to build path