我一直在使用以下指南来开发 hello world REST Web 服务。
该指南可在以下位置找到:http
://wiki.eclipse.org/Creating_a_JAX-RS_Web_Service
我正在尝试打开路径:“ http://localhost:8080/BeezerServer/jaxrs/addresses ”
但它一直说“请求的资源(/BeezerServer/jaxrs/addresses)不可用。”
我的 web.xml:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>BeezerServer</display-name>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.AddressBookApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
该项目如下所示(以防它可能对某人有所帮助):
Tomcat日志:
Aug 31, 2012 7:48:58 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program...
Aug 31, 2012 7:48:59 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 31, 2012 7:48:59 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 31, 2012 7:48:59 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1536 ms
Aug 31, 2012 7:48:59 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 31, 2012 7:48:59 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.29
Aug 31, 2012 7:48:59 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 31, 2012 7:48:59 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 31, 2012 7:48:59 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 737 ms
地址簿应用程序.java:
package com.test;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class AddressBookApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(AddressBook.class);
return classes;
}
}
地址簿.java:
package com.test;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
/**
* A sample resource that provides access to
* server configuration properties
*/
@Path(value="/addresses")
public class AddressBook {
public AddressBook() {
System.out.println();
}
private static String[] list = new String[] {
"Eric- 932 Deloraine Av.",
"Yen - 687 Markham Rd.",
"Keith - 4301 McCowan Rd.",
"Ron - 465 Melrose St.",
"Jane - 35 Cranbrooke Av.",
"Sam - 146 Brooke Av."
};
@GET
@Produces(value="text/plain")
public String getList() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
for (int i = 0; i < list.length; ++i) {
if (i != 0)
buffer.append(", ");
buffer.append(list[i]);
}
buffer.append("}");
return buffer.toString();
}
@GET
@Produces(value="text/plain")
@Path(value="{id}")
public String getPropety(@PathParam("id") int id) {
if (id > -1 && id < list.length -1) {
return list[id];
}
else {
return "Name Not Found";
}
}
@GET
@Produces(value="text/html")
@Path(value="html")
public String getHTMLList()
{
return "<html><body><p><b>Hello</b></body></html>";
}
@POST
@Produces(value="text/html")
@Path(value="form")
public String handlePost(@PathParam("name") String name,@PathParam("age") int age)
{
return "<html><body><p>name: " + name + "<p>age: " + age;
}
}