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

Tli*_*ink 5 java tomcat jersey gradle

我已根据所述的详细信息配置了泽西(使用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>
Run Code Online (Sandbox Code Playgroud)

因为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>
Run Code Online (Sandbox Code Playgroud)

当我在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> ";
  }

}
Run Code Online (Sandbox Code Playgroud)

Tad*_*egn 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)


小智 0

每个方法都需要指定路径

像这样 :

 @GET  
 @Path("testOne")  
 @Produces(MediaType.TEXT_HTML)
    
Run Code Online (Sandbox Code Playgroud)

  • 嘿,谢谢您的回答,但问题似乎源于这样一个事实:我需要为我的 java 文件添加包名称,并在 web.xml 文件中引用该名称` &lt;param-name&gt;jersey.config.server.provider .packages&lt;/param-name&gt; &lt;param-value&gt;CORRECT_PACKAGE_NAME&lt;/param-value&gt;` 。上面我已经将我的java文件放在默认包中。 (3认同)