After making a GET request to a REST service the natural progression is to POST information back to the server. In this episode we will look at how to post json to spring controller and have it automatically convert JSON to arraylist, object or multiple objects.
Detailed Video Notes
The first thing to understand is how json binds to a java object. The
@RequestBody method parameter annotation should bind the json value in the HTTP request body to the java object by using a HttpMessageConverter. In episode 13 how to return XML in REST, we discussed the responsibility of HttpMessageConverter. To recap HttpMessageConverter is responsible for converting the HTTP request message to an assoicated java object. In our case want to convert JSON to a java object when a request is made. Spring will look specifically for a HttpMessageConverter assoicated to the mime type to perform the conversion. Since spring boot configures it automatically if jackson is on our class path MappingJackson2MessageConverter is used. Alternatively you can configure GsonHttpMessageConverter based on google gson library which was offically release in spring version 4.1.
In code we annotate the method parameter with spring @RequestBody which looks like:
Project set up
Creating a project
We will create a new project with spring boot and create a POJO object Car which will post to a spring controller. One thing to note in our snippets is we won't discuss REST api design topic or make the actual update to a persistence layer.
Before we post JSON, lets create a method to return a Car and make a request to http://localhost:8080/. You will notice that we are using
ResponseEntity as the method return type. ResponseEntity is a class that allows you to modify request and response headers. An important design aspect of REST services is also to return the proper HTTP status code and in this case a 200.
Which should return a json response:
Json to java object
You might want to update the Car object by posting json to a URL. A more detailed user story would be, as a user I want to be able update attributes of my car. We will create
@RequestMapping and specify
method = RequestMethod.POST which will tell spring to use this method when a post occurs. When the post is made lets increment the miles by 100.
Using the sample JSON above, lets make a request using advanced rest client chrome plugin and result should increment the miles field by 100.
Json to arraylist
Next, you might have a life event where you inherit a car, get married or your child starts to drive (good luck!). Now we have a series of cars we want to update the mileage. Lets create a new request mapping to http://localhost:8080/cars which accepts a json array as a parameter. Again, we will increment mileage by 100 using a java 8 foreach loop.
Modifing our sample json above we will convert it to a json array and add an additonal node. Lets make a request using advanced rest client chrome plugin and we should see the miles increment by 100.
Passing multiple json objects
If you want to send multiple json objects to a controller, you will need to create a wrapper object that represents your request due to the request containing the entire JSON content. We will create a Truck object, a RequestWrapper object and a new @RequestMapping.
The RequestWrapper will contain a List of Cars and a single truck object.
Making the request
Again, we will use advanced rest client to http://localhost:8080/carsandtrucks with the following JSON.
Thanks for joining in today's level up lunch, have a great day.