繁体   English   中英

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

[英]Web service error HTTP Status 404 - Not Found

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

因此,当我在服务器上运行它时,它将打开index.jsp,然后尝试以下网址,但出现“ HTTP 404错误”

这是我所拥有的
导入了jersey libs的动态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(); 
    }

}

您正在使用旧的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。

尝试:

HTTP://本地主机:8080 / REST / RESTAPI /你好

检查我的情况

规则看起来像这样:/ {url-pattern} / {project} / {path}

我用码头办案

另一个例子

由于以下原因,可能会发生上述问题:

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

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

  1. 第三件事要检查的是在其余类中使用@Path注释定义的路径。
  2. 如果您使用@Paul Samsotha上面建议的球衣罐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由于严重而导致的错误:上下文[/ example]由于先前的错误而启动失败

暂无
暂无

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

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