I have a requirement to convert a POJO to JSON string in a Spring project. I know Spring MVC provide a convenient way to return json in the controller by annotate @ResponseBody, I wonder how does Spring convert pojo to JSON internally? From the Spring MVC maven dependencies hierachy, I find jackson-databind and jackson-core library. While reading the Jackson tutorial, it says different library:
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
And the convert code is similar as below:
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("c:\\user.json"), user);
My questions are:
1: How does spring restful controller convert POJO to JSON? By using Jackson but different dependency library?
2: If I use above Jackson example to convert POJO to JSON, do I have to write the json to file? Is it possible to get the JSON string directly?
3: What's the best way to convert POJO to JSON in Spring project -- Without using @ResponseBody, since I want to convert POJO to JSON and save it to database, @ResponseBody is for restful service, not suitable for my case.
Thanks a lot for your answers in advance.