4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Niaz Ahsan
  • 351
  • 1
  • 7
  • 21
  • 1
    Like Jackson or not, it’s the defacto serializiation library for spring, which itself is the defacto dependency injection library, and spring boot is the goto app platform. Using anything else is hard work. Learn to love the bomb. – Bohemian Aug 11 '19 at 23:30
  • If you're receiving request params from a form post, then you're not receiving JSON and you don't need Jackson. You never *need* Jackson Databind, though if you write client code (e.g. JavaScript in a browser using Ajax) that sends JSON to your server, using Databind to parse the JSON into a POJO does make it *easier*. You do need some kind of JSON parser, because writing your own would be dumb, given you have access to many free ones, but it doesn't have to be Jackson either, you can choose some other JSON library. – Andreas Aug 12 '19 at 00:46
  • Jackson *is what makes `@RequestBody` work*. – chrylis -cautiouslyoptimistic- Aug 12 '19 at 01:02

3 Answers3

3

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

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.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

When you get some request in some data types like json/xml, the Spring MVC platform will try to deserialize this request attributes in some model object of your project.

But the platform itself don't provide a des-serialize implementation out of the box. So it will try to use some des-serializer provider in the classpath like jackson, jersey, gson, etc.

As you said - is possible to use @ModelAttribute - but this annotation is a better option to a request from a form view in the front-end. In cases rest json/xml requests, the @ModelAttribute won't be able to convert correctly the received data to a business class of your program.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
josev.junior
  • 409
  • 2
  • 5
  • 13