(请参阅下面的代码代码)我能够在 Eclipse 环境中的 Tomcat 上运行它,并且它可以正常工作。我已将以下内容导出到一个战争文件并创建了一个 Manifest.MF:
Manifest-Version: 1.0
Main-Class: com.process.Test
当代码在 Eclipse 中运行时,来自服务器端的响应将输出到控制台。
现在终于我的问题(请原谅我的无知,我对此很陌生):
在我的 Tomcat 服务器上部署战争后,如何发送 REST 请求或运行战争并显示服务器响应?
什么相当于实时服务器上的 :http://localhost:8080/rest/xml/list ?
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>com.process.Test</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.process.Test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
客户端代码:
import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
//import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class Test
{
public static void main(String[] args) {
//Instead on using Apache Client
//used default Jersey client
//to send requests
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
String _package = "api.amebatv.com";
WebResource service = client.resource(getBaseURI(_package));
//runRequest(service,"indexpath");
runRequest(service,"list");
}
/*
* For testing purposes the base URI is :http://localhost:8080/package name
* Will change to domain name for production code
*/
private static URI getBaseURI(String _package){
return UriBuilder.fromUri(
"http://localhost:8080/api.process.com").build();
}
private static void runRequest(WebResource service,String path){
String response = service.path("rest/xml/"+path).accept(MediaType.APPLICATION_XML).get(String.class);
System.out.println("Post Response :"+response);
}
}
服务器端:
@Path("/xml")
public class Service {
// List of video objects
private ArrayList<Video> videolist;
//Parser object, parses xml into video objects
private Parser parser = new Parser();
/*
* Constructor
* Parse XML and create video list on call
*/
public Service(){
parser.createXML();
videolist = parser.getList();
}
/************************************************
*GET
* Path: /indexpath
* list of all only <video> items
* @return : ArrayList of Video objects
***********************************************/
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_XML)
public List<Video> getCustomerInXML()
{
return videolist;
}
}