【问题标题】:Web service error HTTP Status 404 - Not FoundWeb 服务错误 HTTP 状态 404 - 未找到
【发布时间】:2015-12-29 13:25:59
【问题描述】:

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

所以,当我在服务器上运行它时,它会打开 index.jsp,然后我尝试以下 url,但我收到“HTTP 404 错误”

这就是我所拥有的
导入了球衣库的动态 Web 项目。 关于这一点的注释 - 我收到了一个找不到类的错误,并看到我必须使用 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>

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

}

【问题讨论】:

    标签: java eclipse rest jersey


    【解决方案1】:

    出现上述问题的原因如下:

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

    &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;

    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>
    
    1. 当您的服务器已启动且没有任何错误但您的应用程序的上下文没有出现时,可能会出现此错误。您可以在日志中查看。有关更多信息,请参阅此链接 ---> 404 error due to SEVERE: Context [/example] startup failed due to previous errors

    【讨论】:

      【解决方案2】:

      尝试:

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

      检查my case

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

      我用码头处理我的案子

      another example

      【讨论】:

        【解决方案3】:

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

        com.sun.jersey.config.property.packages
        

        对于 Jersey 2.x 应该是

        jersey.config.server.provider.packages
        

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

        【讨论】:

          猜你喜欢
          • 2012-08-17
          • 2016-03-25
          • 2021-11-29
          • 2020-07-31
          • 2020-03-20
          • 2023-03-03
          • 2019-12-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多