Java jersey/tomcat 描述 源服务器没有找到目标资源的当前表示

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

jersey/tomcat Description The origin server did not find a current representation for the target resource

javatomcatgradlejersey

提问by Tlink

I have configured jersey (with tomcat server) according to the details described until step 6.3 here: http://www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup

我已经根据此处的步骤 6.3 之前描述的详细信息配置了球衣(使用 tomcat 服务器):http: //www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup

Below is my web.xml file, the only difference is that the path/package name for the jersey.config.server.provider.packages

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

the instructions has;

指示有;

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

because com.vogella.jersey.firstis their package name, while I chose to point mine to my default package (jerseytest)

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

but this is my web.xml

但这是我的 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>

when I run the application on the tomcat server I keep getting:

当我在 tomcat 服务器上运行应用程序时,我不断收到:

HTTP Status 404 – Not Found

Type Status Report

Message /jerseytest/

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.23

HTTP 状态 404 – 未找到

类型状态报告

Message /jerseytest/

描述 源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。

Apache Tomcat/8.5.23

and when i point the browser to the end point mentioned in the tutorial : http://localhost:8080/com.vogella.jersey.first/rest/hello

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

I get a not found error:

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

HTTP Status 404 – Not Found

Type Status Report

Message Not Found

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.23

HTTP 状态 404 – 未找到

类型状态报告

Message Not Found

描述 源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。

Apache Tomcat/8.5.23

When I point the browser directly to http://localhost:8080/rest/hello

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

I get:

我得到:

HTTP Status 404 – Not Found

Type Status Report

Message /rest/hello

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.23

HTTP 状态 404 – 未找到

类型状态报告

Message /rest/hello

描述 源服务器没有找到目标资源的当前表示,或者不愿意透露一个存在。

Apache Tomcat/8.5.23

My java class is identical to the example in the instructions , except that mine is in the default package :

我的 java 类与说明中的示例相同,只是我的 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> ";
  }

}

回答by sunil

Each method needs to specify the path

每个方法都需要指定路径

like this :

像这样 :

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

回答by Tadele Ayelegn

I struggled with this problem many times.

我在这个问题上挣扎了很多次。

The solution I am currently using is weather the webapp(or the folder where you kept the views like jsp) is under deployment assembly.

我目前使用的解决方案是 web 应用程序(或您保存视图如 jsp 的文件夹)处于部署程序集之下。

To do so Right click on the 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)

为此,右键单击 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)