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

时间:2015-08-12 18:26:38

标签: eclipse rest tomcat7 jersey-2.0 rad

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

当我输入:http://localhost:8080/时,我获得了Apache主页,因此服务器正在运行,但是http://localhost:8080/jersey/rest/hellohttp://localhost:8080/jersey/WEB-INF/classes/jersey/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> ";
      }
}

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

版本:

  • Tomcat:7.0.663
  • RAD:8.5
  • 泽西岛:2.19

谢谢,

回应Maciej 这有效!我需要使用<servlet-mapping>的网址格式添加/*。然后使用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>

4 个答案:

答案 0 :(得分:6)

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

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

设置为:base URL + /your_pathbase URL基于您的应用程序名称,servlet和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>

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

即使按照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>

这对我有用。

答案 3 :(得分:0)

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

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

 2. Then click add to build path