iCode iCode - 1 year ago 107
JSON Question

Spring MVC controller Test - print the result JSON String

Hi I have a Spring mvc controller

@RequestMapping(value = "/jobsdetails/{userId}", method = RequestMethod.GET)
public List<Jobs> jobsDetails(@PathVariable Integer userId,HttpServletResponse response) throws IOException {
try {
Map<String, Object> queryParams=new LinkedHashMap<String, Object>();

queryParams.put("userId", userId);

jobs=jobsService.findByNamedQuery("findJobsByUserId", queryParams);

} catch(Exception e) {
return jobs;

I want to see how the JSON String will looks like when I run this. I wrote this test case

@ContextConfiguration(locations = {"classpath:test-applicationcontext.xml"})
public class FindJobsControllerTest {
private MockMvc springMvc;

WebApplicationContext wContext;

public void init() throws Exception {
springMvc = MockMvcBuilders.webAppContextSetup(wContext).build();

public void documentsPollingTest() throws Exception {
ResultActions resultActions = springMvc.perform(MockMvcRequestBuilders.get("/jobsdetails/2").accept(MediaType.APPLICATION_JSON));

System.out.println(/* Print the JSON String */); //How ?

How to get the JSON string?

I am using Spring 3, codehause Jackson 1.8.4

Answer Source

Try this code:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download