Java 返回 http 状态 404 的简单 Rest Web 服务

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

Simple Rest Webservice returning http status 404

javaeclipseresttomcatjersey-2.0

提问by SvenK

I have been trying to get this tutorial to work: LinkI am using Apache Tomcat 7.0 and the Jersey 2.0 libraries. This is my service:

我一直在努力让本教程发挥作用: 链接我正在使用 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>";  
  }  
}

and 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" 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>

I tried to Run it on server to deploy and I also tried to let eclipse export it as a war file and then deploy it with the tomcat application manager. Both ways I get the HTTP Status 404, The requested resource is not available. Prior to this there is error message in any logs. I have also tried to put a simple index.html file in the Webcontent folder, but I could also not access that in the browser. I know that there are a lot of similar posts on the forum, but after having read them and hours of trying i still cannot figure out how to solve my problem.

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

采纳答案by jeremyjjbrown

Sometimes troubleshooting java webservices is frustrating so it is helpful to follow some simple steps to find the error.

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

  1. Is your firewall on? Check that it is off or not blocking the port your webserver running on. Most of the time it is port 8080. It is in the url localhost:8080/mywebservice/url

  2. Check the root application page supplied by your server to make sure the server is running at all. On Tomcat that usually localhost:8080/manager/html

  3. Check the logs of your application container for a stacktraceon startup to make sure your deployment is not in error.

  4. Try to deploy a minimum possible webservice such as "Hello World" jax-rsto make sure you have the correct libraries and other configurations available.

  5. If you pass all of those and still no webservice your jax-rs annotations are probably incorrect in some manner. You can enable Request Matching in your web.xml by adding to the configuration.

    <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>

  1. 你的防火墙打开了吗?检查它是否关闭或没有阻止您的网络服务器运行的端口。大多数时候是8080端口。它在url localhost: 8080/mywebservice/url

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

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

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

  5. 如果您通过了所有这些但仍然没有网络服务,您的 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>

You'll see url traces in your log

您将在日志中看到 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"],

hint:probably not a good idea to leave this on in production

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

回答by GPopat

Please enable spring logging. It will print all the @RequestMapping in the startup like below

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

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

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