Simple Rest Webservice返回http状态404

Sve*_*enK 6 java eclipse rest tomcat jersey-2.0

我一直试图让本教程工作: 链接 我正在使用Apache Tomcat 7.0和Jersey 2.0库.这是我的服务:

package org.arpit.javapostsforlearning.webservice;

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

@Path("ConversionService")  
public class FeetToInchAndInchToFeetConversionService {  
 @GET  
 @Path("/InchToFeet/{i}")  
  @Produces(MediaType.TEXT_XML)  
  public String convertInchToFeet(@PathParam("i") int i) {  

    int inch=i;  
    double feet = 0;  
    feet =(double) inch/12;  

    return "<InchToFeetService>"  
    + "<Inch>" + inch + "</Inch>"  
      + "<Feet>" + feet + "</Feet>"  
     + "</InchToFeetService>";  
  }  

  @Path("/FeetToInch/{f}")  
  @GET  
  @Produces(MediaType.TEXT_XML)  
  public String convertFeetToInch(@PathParam("f") int f) {  
   int inch=0;  
      int feet = f;  
      inch = 12*feet;  

      return "<FeetToInchService>"  
        + "<Feet>" + feet + "</Feet>"  
        + "<Inch>" + inch + "</Inch>"  
        + "</FeetToInchService>";  
  }  
}
Run Code Online (Sandbox Code Playgroud)

这是我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 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>RESTfulWebServiceExample</display-name>  
<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>org.arpit.javapostsforlearning.webservice</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)

我试图在服务器上运行它来部署,我也尝试让eclipse将其导出为war文件,然后使用tomcat应用程序管理器进行部署.我获得HTTP状态404的两种方式,请求的资源都不可用.在此之前,任何日志中都有错误消息.我还尝试在Webcontent文件夹中放置一个简单的index.html文件,但我也无法在浏览器中访问它.我知道在论坛上有很多类似的帖子,但在阅读完它们并且尝试了几个小时后,我仍然无法弄清楚如何解决我的问题.

jer*_*own 5

有时对java webservices进行故障排除令人沮丧,因此按照一些简单的步骤查找错误会很有帮助.

  1. 你的防火墙开着吗?检查它是否已关闭或未阻止运行Web服务器的端口.大多数情况下它是端口8080.它位于url localhost:8080/ mywebservice/url中

  2. 检查服务器提供的根应用程序页面,确保服务器正在运行.在Tomcat上,通常是localhost:8080/manager/html

  3. 在启动时检查应用程序容器的日志以查找堆栈跟踪,以确保部署没有错误.

  4. 尝试部署尽可能少的Web服务,例如"Hello World"jax-rs,以确保您具有正确的库和其他可用配置.

  5. 如果你通过所有这些仍然没有web服务,你的jax-rs注释可能在某种程度上是不正确的.您可以通过添加配置在web.xml中启用请求匹配.

    <web-app> <servlet> <servlet-name>Jersey REST Service for value codes</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> ... <init-param> <param-name>com.sun.jersey.config.feature.Trace</param-name> <param-value>true</param-value> </init-param> </servlet> ... </web-app>

您将在日志中看到URL跟踪

{X-Jersey-Trace-008=[mapped exception to response: javax.ws.rs.WebApplicationException@56f9659d -> 415 (Unsupported Media Type)],
X-Jersey-Trace-002=[accept right hand path java.util.regex.Matcher[pattern=/myResource/([-0-9a-zA-Z_]+)(/.*)? region=0,17 lastmatch=/myResource/23/mySubresources]: "/myResource/23/mySubresources" -> "/myResource/23" : "/mySubresources"],
Run Code Online (Sandbox Code Playgroud)

提示:将这个留在生产中可能不是一个好主意