(see code below). I can run it on Tomcat in an eclipse environment and it works as it should. I exported the following to a military file and created Manifest.MF with:
Manifest-Version: 1.0
Main-Class: com.process.Test
When the code runs in Eclipse, the server-side response is output to the console.
Now finally my question (Excuse my ignorance, I'm pretty new to this):
Once the war is deployed on my Tomcat server, how do I send a REST request or start a war and display the server response?
Which is equivalent: http: // localhost: 8080 / rest / xml / list on a real server?
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>
Client Code:
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.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) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
String _package = "api.amebatv.com";
WebResource service = client.resource(getBaseURI(_package));
runRequest(service,"list");
}
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);
}
}
Server side:
@Path("/xml")
public class Service {
private ArrayList<Video> videolist;
private Parser parser = new Parser();
public Service(){
parser.createXML();
videolist = parser.getList();
}
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_XML)
public List<Video> getCustomerInXML()
{
return videolist;
}
}
Fabii source
share