Solved

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

Posted on 2016-07-26
3
38 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
Comment Utility
could try turning up the logging level.
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now