繁体   English   中英

Simple Rest Webservice返回http状态404

[英]Simple Rest Webservice returning http status 404

我一直试图让本教程工作: 链接我正在使用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>";  
  }  
}

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

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

有时对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"],

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

请启用弹簧记录。 它将在启动时打印所有@RequestMapping,如下所示

INFO 2017-09-21 10:41:35,822(AbstractHandlerMethodMapping.java:531) - 映射“{[/ myapi / cal / calsalary],methods = [GET || PUT],params = [configName && configUserId && userId && asId ]}“上市

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM