我的web.xml文件: 的 Web.xml中 我正在尝试做一个简单的球衣restful webservice你好世界,但它无法正常工作。任何人都可以检查我做错了吗?
我使用以下网址:
http://localhost:8080/learning1/rest/firstRest/User1
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Learn</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.java.learning1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Learn</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我的restfulwebservice java文件: java文件 服务器正在响应HTTP状态代码404:未找到
package org.java.learning1;
import javax.websocket.server.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
@Path("/firstRest")
public class firstRest {
@GET
@Produces("MediaType.TEXT_HTML")
@Path("{name}")
public String sendResponse(@Context HttpHeaders httpHeaders, @PathParam("name") String name){
String greeting = "hello";
return greeting;
}
}
答案 0 :(得分:0)
Hello user7481861,欢迎来到stackoverflow!
您的代码中有两个错误,我已经对其进行了更正,并在每个错误中添加了注释,说明错误。
package org.java.learning1;
//import javax.websocket.server.PathParam; <-- incorrect import
import javax.ws.rs.PathParam; // <<-- correct import
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
@Path("/firstRest")
public class firstRest {
@GET
@Produces(MediaType.TEXT_HTML) // No quotes like stdunbar said in the comments
@Path("/{name}") // missing slash before name
public String sendResponse(@Context HttpHeaders httpHeaders, @PathParam("name") String name){
String greeting = "hello " + name; // concatenate the string with the variable name
return greeting;
}
}