嗨,我有一个Spring mvc控制器

@RequestMapping(value = "/jobsdetails/{userId}", method = RequestMethod.GET)
@ResponseBody
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) {
        logger.debug(e.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
    return jobs;
}

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

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

    @Autowired
    WebApplicationContext wContext;

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

    @Test
    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 ?
    }
}

如何获取JSON字符串?

I am using Spring 3, codehause Jackson 1.8.4

推荐答案

Try this code:

resultActions.andDo(MockMvcResultHandlers.print());

Json相关问答推荐

使用JOLT转换来卸载数据

Python将Pandas转换为嵌套的JSON

NiFi QueryRecord处理器- Select 可选的JSON属性

419(未知状态)使用laravel处理PUT请求

展平多个数组以保持顺序

Jolt 规范将父对象中的所有键应用于数组中的所有对象

jq: Select 何时来自另一个数组的值与此 json 中的值匹配

从 JSON 响应中获取最新版本发布字段

在 postgresql 中将行转换为 json 对象

如何将西里尔字母转换为 utf16

Jackson 的@JsonView、@JsonFilter 和 Spring

在 Rails 中使用 JSON 创建嵌套对象

使用 simplejson 序列化简单类对象的最简单方法?

jQuery循环.each()JSON键/值不起作用

使用适用于 Python 的 Google API - 我从哪里获取 client_secrets.json 文件?

在 JSON.NET 中序列化派生类时的字段顺序

有没有办法折叠 Postman 中的所有 json 字段

如何从 MySQL 中检索 JSON 数据?

使用 JSONArray 和 JSONObject 进行 Foreach

使用 JSON.NET 序列化/反序列化对象字典