我正在遵循一个简单的 Web 服务教程,但似乎无法与 Java 代码交互。我怀疑我的 web.xml 有错误,但我不确定。没有明显的错误,index.jsp 是服务器,没有任何问题。
因此,当我在服务器上运行它时,它会打开index.jsp,然后我尝试以下网址,但收到“HTTP 404 错误”
- http://localhost:8080/RestApi/ - 有效,显示 html 页面
- http://localhost:8080/RestApi/rest - http 404错误
- http://localhost:8080/RestApi/rest/hello - http 404错误
- http://localhost:8080/RestApi/rest/hello/somevalue - http 404错误
这是我所拥有的
导入了 Jersey 库的动态 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。
关于java - Web 服务错误 HTTP 状态 404 - 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32888461/