Friday, September 19, 2014

How to write POST method in RESTful Java using Jersey?

Introduction:

In our previous articles, we have seen how to write basic REST services using Jersey framework. And also we saw how to write REST client using Jersey client API. In this article we are going to see, how to write POST method and how to consume the API.

Implementation:

POST method is a special method and it being used interchangeably based on the situation. Meaning some will use POST method for updating an object and some people are using POST method for creating an object. It is up to the user who can decide the situation based on their need.

As you guessed so, the POST method is annotated with @POST annotation. In the below example there are 2 variables are passed from the client.
title
author
Both these parameters are annotated with @FormParam annotation.

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public String createBook(@FormParam(value = "title") String title, @FormParam(value = "author") String author) {
Book book = new Book(title, author);
Gson gson = new Gson(); //Gson is used to simplify the JSON generation process
return gson.toJson(book); //Just send back the JSON to the client. But user can do anything...
}

Client code:

JAX-RS and Jersey have provided lot of useful API's to write REST client code.

//Setting the post method url to the client
WebTarget webTarget = client.target("http://localhost:8080/restfullab/api").path("book");

//Add key-value pair into the form object
Form form = new Form();
form.param("title", "RESTful Java with JAX-RS 2.0 ");
form.param("author", "Bill Burke");

//Send the form object along with the post call
Response response = webTarget.request().post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));
System.out.println("Respose code: " +  response.getStatus());
System.out.println("Respose value: " + response.readEntity(String.class));

The parameters will be passed through 'Form' values as key-value pair. 'Key' should match with the @FormParam annotation value.

I hope you have enjoyed reading this post. Please give your valuable feedback or comments if you have any...

We will meet again in few days in another article. Until then enjoy hacking RESTful Java and Jersey framework! 

Source references:

Working source code is available under the following location in Github. Fork and enjoy...
https://github.com/LiquidLab/restfullab/blob/master/src/com/liquidlab/restfullab/resources/BookResource.java
https://github.com/LiquidLab/restfullab/blob/master/test/com/liquidlab/restfullab/resources/test/BookResourceTest.java

Refer Jersey Javadoc for further details...

8 comments:

  1. Simple but powerful. This helped me a lot

    ReplyDelete
  2. The parameters will be passed through 'Form' values as key-value pair. 'Key' should match with the @FormParam annotation value.

    Can the values here be objects ??

    ReplyDelete
  3. I want to send custom java objects as values using @FormParam annotation. Can I do that ??

    ReplyDelete
  4. I think you have to write custom provider to achieve this. I will prepare some sample and post it here.

    ReplyDelete
  5. I wanted this POST returning me a certain Object, that I know my target returns a JSON... how could I do it?

    ReplyDelete
  6. I wanted this POST returning me a certain Object, that I know my target returns a JSON... how could I do it?

    ReplyDelete
  7. Java.net is moving to a new location in April 2017. All administrators, please copy your project resources before closing java.net

    ReplyDelete