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

Web service error HTTP Status 404 - Not Found

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

所以,当我在服务器上 运行 它时,它会打开 index.jsp 然后我尝试以下网址,但我得到 'HTTP 404 Errors'

这是我的
导入了 jersey 库的动态 web 项目。 关于这一点的注释 - 我收到 class not found 的错误,看到我必须使用 Glassfish.org... 而不是 com.sun 一个,不知道为什么,但是有你去吧

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

我的javaclass如下。没有错误。

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(); 
    }

}

您使用的是旧球衣1.x属性

com.sun.jersey.config.property.packages

对于泽西岛 2.x 应该是

jersey.config.server.provider.packages

作为一般规则,任何东西 com.sun.jersey 代表泽西岛 1.x,org.glassfish.jersey 代表 2.x .

尝试:

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

检查my case

规则是这样的:/{url-pattern}/{project}/{path}

我运行我的码头案例

another example

上述问题可能是由于以下原因造成的:

  1. 首先检查部署在 tomcat 的 webapps 文件夹中的应用程序的名称,它是否与您的 url 匹配,在上面的情况下给出 404.But,因为它显示的是欢迎页面,所以这里不关心。
  2. 检查 web.xml 中提及的 url 模式,因为它必须与您正在点击的 url 中的模式相同。

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

  1. 第三件要检查的是在其余部分 class 中定义的带有 @Path 注释的路径。
  2. 如果您使用 jersey jars 2.x,请检查 web.xml 以获取以下条目,如上文@Paul Samsotha
  3. 所建议
<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. 当您的服务器已正常启动但您的应用程序上下文尚未启动时,可能会出现此错误。您可以在 logs.For 中查看更多信息,请参阅此 Link ---> 404 error due to SEVERE: Context [/example] startup failed due to previous errors