Why do we need jackson databind?

六眼飞鱼酱① 提交于 2021-02-10 11:50:22

问题


I am new in JAVA EE. 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.


回答1:


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.




回答2:


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.




回答3:


When you get some request in some data types like json/xml, the Java EE 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.



来源:https://stackoverflow.com/questions/57454213/why-do-we-need-jackson-databind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!