【问题标题】:jersey/tomcat Description The origin server did not find a current representation for the target resourcejersey/tomcat 描述 源服务器没有找到目标资源的当前表示
【发布时间】:2018-05-15 01:37:35
【问题描述】:

我已经根据此处步骤 6.3 之前描述的详细信息配置了球衣(带有 tomcat 服务器): 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> ";
  }

}

【问题讨论】:

  • 尝试通过转到 project->property-facets 来添加和更改项目的方面

标签: java tomcat gradle jersey


【解决方案1】:

我多次为这个问题苦苦挣扎。

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

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

【讨论】:

    【解决方案2】:

    每个方法都需要指定路径

    像这样:

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

    【讨论】:

    • 嘿,谢谢你的回答,但问题似乎源于我需要为我的 java 文件添加一个包名,并在 web.xml 文件中引用该名称` jersey.config.server.provider.packagesCORRECT_PACKAGE_NAME` .上面我已经将我的java文件放在了默认包中。
    猜你喜欢
    • 2020-10-03
    • 2017-10-06
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多