eclipse Web 服务错误 HTTP 状态 404 - 未找到

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

Web service error HTTP Status 404 - Not Found

javaeclipserestjersey

提问by Fearghal

I am following a simple webservice tutorial and can't seem to interact with the Java code. I suspect my web.xml has an error but I'm not sure. There are no obvious errors and the index.jsp is server without any problems.

我正在关注一个简​​单的网络服务教程,但似乎无法与 Java 代码进行交互。我怀疑我的 web.xml 有错误,但我不确定。没有明显的错误并且 index.jsp 是服务器,没有任何问题。

So, when I'm running it on the server, it opens index.jsp and I then try the following urls, but I'm getting 'HTTP 404 Errors'

因此,当我在服务器上运行它时,它会打开 index.jsp,然后我尝试使用以下网址,但我收到了“HTTP 404 错误”

Here is what i have
Dynamic web project with jersey libs imported. A note on this - I got an error for class not found and saw that I had to use Glassfish.org... instead of the com.sun one, don't know why, but there ya go. enter image description here

这是我
导入了球衣库的动态 Web 项目。关于这一点的说明 - 我在找不到类时遇到错误,看到我必须使用 Glassfish.org... 而不是 com.sun 一个,不知道为什么,但是你去了。 enter image description here

My web.xml is as follows. No errors.

我的 web.xml 如下。没有错误。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>RestApi</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <display-name>Rest Web Services App by me</display-name>
  <servlet>
    <servlet-name>exampleServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.rest.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>exampleServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

My java class is as follows. No errors.

我的java类如下。没有错误。

package com.rest.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorld {
    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg){
        String output = "Welcome to the world of Rest : "+msg;
        return Response.status(200).entity(output).build(); 
    }

}

采纳答案by Paul Samsotha

You are using the old Jersey 1.x property

您正在使用旧的 Jersey 1.x 属性

com.sun.jersey.config.property.packages

For Jersey 2.x it should be

对于 Jersey 2.x 应该是

jersey.config.server.provider.packages

As a general rule, anythingwhere you see com.sun.jerseyis for Jersey 1.x and org.glassfish.jerseyis for 2.x.

作为一般规则,您看到的任何内容com.sun.jersey都适用于 Jersey 1.x 和org.glassfish.jersey2.x。

回答by no7dw

try:

尝试:

http://localhost:8080/rest/RestApi/hello

http://localhost:8080/rest/RestApi/hello

check my case

检查我的情况

the rule seems like this: /{url-pattern}/{project}/{path}

规则如下:/{url-pattern}/{project}/{path}

I run my case with jetty

我用码头处理我的案子

another example

另一个例子

回答by Prakhar Agrawal

the above issue can occur due to following reasons :

出现上述问题可能是由于以下原因:

  1. First check the name of your application deployed in the webapps folder of the tomcat, whether it is matching your url or not that is giving 404.But in the above case, as it is showing the welcome page, then it is not the concern here.
  2. Check the url pattern mention in the web.xml as it must be the same as in the url you are hitting.
  1. 首先检查部署在tomcat的webapps文件夹中的应用程序的名称,是否与您的url匹配,即给出404。但在上述情况下,因为它显示了欢迎页面,所以这里不关心.
  2. 检查 web.xml 中提及的 url 模式,因为它必须与您点击的 url 中的相同。

<url-pattern>/rest/*</url-pattern>

<url-pattern>/rest/*</url-pattern>

  1. Third thing to check is that the path defined in the rest class with @Path annotation.
  2. Check the web.xml for following entries if you are using jersey jars 2.x as suggested above by @Paul Samsotha
  1. 要检查的第三件事是在带有@Path 注释的其余类中定义的路径。
  2. 如果您按照@Paul Samsotha 的上述建议使用 jersey jars 2.x,请检查 web.xml 中的以下条目
<servlet> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

      <init-param> 
         <param-name>jersey.config.server.provider.packages</param-name> 
         <param-value>com.pizzeria</param-value> 
      </init-param> 
   </servlet>
<servlet> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

      <init-param> 
         <param-name>jersey.config.server.provider.packages</param-name> 
         <param-value>com.pizzeria</param-value> 
      </init-param> 
   </servlet>
  1. There may be a possibility to get this error when your server has been up without any errors but the context of your application hasn't. You can check it in the logs.For further information please refer this Link ---> 404 error due to SEVERE: Context [/example] startup failed due to previous errors
  1. 当您的服务器已启动且没有任何错误但您的应用程序的上下文没有时,可能会出现此错误。您可以在日志中检查它。有关更多信息,请参阅此链接 ---> 404 错误由于严重:上下文 [/示例] 启动失败由于先前的错误