tags:

views:

48

answers:

1

Hello Everyone,

I want to create a sample RESTful web service in java which involves all the four CRUD operations and I deployed it in tomcat. I used JAX-RS (Jersey) library to implement this in java. As of now , I can call the GET method to retrieve the list of records and display it. But I don know how to call the POST, PUT and DELETE method. Can anyone tell how to call those methods ?

A: 

Give this a shot, you can extract the key/value pairs on the server side pretty easily:

ClientConfig config = new DefaultClientConfig();
com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create(config);

MultiValueMap formData = new MultiValueMapImpl();
formData.add("key", "value");

WebResource resource = client.resource("http://path/to/resource");
ClientResponse response = resource.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);
MStodd
Thank you. Its working for me. I have one more doubt. Is there any other way we can call those methods like directly modifying the url (passing the parameters in url) in the browser itself?
Senthil
Not by putting them in the URL, that's always going to result in a GET, I believe. You'd have to use javascript and do a POST or whatever that way, or construct a form to fill out with method="post" and hit the submit button.
MStodd
Thank you very much MStodd for the quick response. I got cleared now.
Senthil
Hello Mstodd. I have one more doubt. I have PUT method or POST method which consumes the json as mediatype in the REST web service. Can I know how to call these methods from the client side. How do we pass that json as input to those PUT and POST method. If we want to consume xml, then we are using JAXBElement.Similarly for consuming json, what to do ?
Senthil
Well, as the second parameter to the post call, you can pass in any Object you want. Not being an expert you could probably just construct your XML or JSON and pass it in as the second parameter as a String.
MStodd
Thank you MStodd for your response. It is working for me now. I am using the instance of JSONObject class of Jettison library to pass the input values. Now I am able to call the POST and PUT method which consumes the JSON as input. Well as you said, If we pass the xml or json by constructing it as string, how would we receive it on the server side. I mean with the same string object ? Also what are all the other possible ways we can send the input values to consume the json format ?
Senthil