jersey / tomcat说明源服务器未找到目标资源的当前表示

时间:2017-12-01 00:56:11

标签: java tomcat gradle jersey

我已按照此处描述的详细信息配置了泽西(使用tomcat服务器),直到此处的步骤6.3: http://www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup

下面是我的web.xml文件,唯一的区别是jersey.config.server.provider.packages

的路径/包名称

说明书已经;

<param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.jersey.first</param-value>

因为com.vogella.jersey.first是他们的包名,而我选择指向我的默认包(jerseytest)

但这是我的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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>jerseytest</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>jerseytest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

当我在tomcat服务器上运行应用程序时,我一直得到:

  

HTTP状态404 - 未找到

     

输入状态报告

     

Message /jerseytest/

     

描述源服务器未找到当前表示   对于目标资源或不愿意披露存在的资源。

     

Apache Tomcat / 8.5.23

当我将浏览器指向教程中提到的结束点时: http://localhost:8080/com.vogella.jersey.first/rest/hello

我收到一个未找到的错误:

  

HTTP状态404 - 未找到

     

输入状态报告

     

Message Not Found

     

描述源服务器未找到当前表示   对于目标资源或不愿意披露存在的资源。

     

Apache Tomcat / 8.5.23

当我将浏览器直接指向http://localhost:8080/rest/hello

我明白了:

  

HTTP状态404 - 未找到

     

输入状态报告

     

Message /rest/hello

     

描述源服务器未找到当前表示   对于目标资源或不愿意披露存在的资源。

     

Apache Tomcat / 8.5.23

我的java类与说明中的示例相同,不同之处在于我的默认包:

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

// Plain old Java Object it does not extend as class or implements
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML.

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@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>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

}

2 个答案:

答案 0 :(得分:5)

我多次挣扎于这个问题。

我目前使用的解决方案是使用webapp(或保存jsp等视图的文件夹)处于部署程序集中。

要这样做 右键单击project > Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder > (add your jsp folder. In default case it is src/main/webapp)

答案 1 :(得分:0)

每种方法都需要指定路径

像这样:

 @GET  
 @Path("testOne")  
 @Produces(MediaType.TEXT_HTML)