I am successfully created restful web service and deploy it in Apache Tomcat 7.0. After successful deployment I start my server. By using the below command i invoke the web service.
WebResource resource = client.resource("http://localhost:8080/rest/samp/create");
My web method is
@POST
@Path("/create")
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.TEXT_XML)
public final String sample(final String xmlMessage) {
return "<xml version=1.0><welcome>"+xmlmessage+"</welcome>";
}
Here I am passing XML content as argument and get the XML content as response.
Now what I need is how to pass the XML content to the web method.
I’m guessing that the library in use here is Jersey.
You have to use a builder to set the appropriate HTTP headers, method and entity body.
But I recommend using JAXB instead. Creating XML as plain strings is just crude and unnecessarily annoying. It doesn’t show in such a simple example (grabbing a whole XML and wrapping it with another tag) but it will soon enough.
This tutorial should get you started.