Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need jackson databind?

I am new in Spring MVC. My question is, why do we need jackson databind? Because We can receive the Request Params by @ModelAttribute and requests through http PUT or POST by @RequestBody. I can't find a reason why we need jackson databind to convert json/xml to POJO or vice versa.

Thanks.

like image 912
Niaz Ahsan Avatar asked Oct 14 '25 17:10

Niaz Ahsan


2 Answers

By default, when an endpoint expects a JSON document as input and a given controller method argument is annotated with @RequestBody, Spring will use Jackson databind features to map the incoming JSON document to a Java object. You don't need to use the Jackson's ObjectMapper directly, as Spring does it for you.

For example purposes, consider the following HTTP request to create a comment:

POST /comments HTTP/1.1
Host: example.org
Content-Type: application/json

{
  "content": "Lorem ipsum"
}

And the following class which represents a comment:

@Data
public class Comment {
    private String content;
}

A @RestController to handle such request would be like:

@RestController
@RequestMapping("/comments")
public class CommentController {

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Foo> createComment(@RequestBody Comment comment) {

        // By default, Spring will rely on Jackson databind to map the incoming 
        // JSON document to the comment argument annotated with @RequestBody

        ...
    }
}

If you are interested in the Spring component that maps the incoming JSON document to a Java object, have a look at the MappingJackson2HttpMessageConverter class:

Implementation of HttpMessageConverter that can read and write JSON using Jackson 2.x's ObjectMapper.

This converter can be used to bind to typed beans, or untyped HashMap instances.

By default, this converter supports application/json and application/*+json with UTF-8 character set. [...]


If you are creating a HTTP API and exposing resources that can be manipulated with JSON representations, it's unlikely you'll use @ModelAtribute. Such annotation is particularly useful when you are dealing with web views.

like image 58
cassiomolin Avatar answered Oct 17 '25 09:10

cassiomolin


Why do we need jackson databind?

Because representing structured data is much easier using XML (or JSON) than using simple name-value pairs.

Because it is more convenient to send and receive JSON from the client side when you are doing AJAX.

Because once you have to deal with sending and receiving JSON or XML in the server side Java app, it is more convenient to deal with structured data as POJOs.

None of the above points mean you have to use a binding. There are other ways of dealing with each of the above. But many Java developers think that data bindings the better way to go: more efficient in terms of developer time, and more reliable. Especially if you are implementing services with a complex APIs. That's why they are popular.


And as other answers/comments point out, if you are using @RequestBody, then that is using a binding library under the hood to give you the POJOs. In the case of Spring, it is Jackson that is being used.

like image 36
3 revsStephen C Avatar answered Oct 17 '25 10:10

3 revsStephen C