Solved

How does spring convert a Json string that is being passed to an object

Posted on 2016-07-26
3
52 Views
Last Modified: 2016-08-09
Hi,
I am making the following post request : http://localhost:8080/api/users/create
with postman and body :
{ "userName" : "rohit", "email" : "rohit@xyz.com" }

In my controller i have :
@RequestMapping(value = "/api/users/create", method = RequestMethod.POST)
    @ResponseBody
    public APIPOSTResponse userAdd(@RequestBody User user)

And this perfectly converts json to the User object.
I read in docs that spring using a HttpMessageConverters for this.
When i navigated from intellij to MessageConverter interface i saw many classes implementing it.
They were all in spring-web jar.
I found one implementation which i suspected : MappingJackson2HttpMessageConverter will do the conversion and put break points in the decompiled source code the debugger didnt stop. Although it did stop
in the class AbstractHttpMessageConverter.
My understanding is based on the mediaType proper converter will be invoked. Want to confirm if this is correct with experts. Also what converter is executed for mediaType application/json ?

Thanks
0
Comment
Question by:Rohit Bajaj
3 Comments
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 250 total points
ID: 41729905
could try turning up the logging level.
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 41730518
Want to confirm if this is correct with experts.

Essentially correct, yes. There are a few added things like even after a Converter is matched on media type, it can still have a say as to whether it will perform the conversion or not (check the canRead, canWrite methods)

Note, there is also a MappingJacksonHttpMessageConverter (note it doesn't have the 2) that depending on your version of Jackson that you're using, this one may be getting used.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question