【发布时间】:2015-12-29 13:25:59
【问题描述】:
我正在学习一个简单的 Web 服务教程,但似乎无法与 Java 代码进行交互。我怀疑我的 web.xml 有错误,但我不确定。没有明显的错误,index.jsp 是 server 没有任何问题。
所以,当我在服务器上运行它时,它会打开 index.jsp,然后我尝试以下 url,但我收到“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 错误
这就是我所拥有的
导入了球衣库的动态 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();
}
}
【问题讨论】: