gpt4 book ai didi

java - Spring MVC Controller 测试 - 打印结果 JSON 字符串

转载 作者:IT老高 更新时间:2023-10-28 12:44:58 25 4
gpt4 key购买 nike

您好,我有一个 Spring mvc Controller

@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;
}

我想看看运行它时 JSON 字符串的样子。我写了这个测试用例

@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 字符串?

我正在使用 Spring 3,codehause Jackson 1.8.4

最佳答案

试试这个代码:

resultActions.andDo(MockMvcResultHandlers.print());

关于java - Spring MVC Controller 测试 - 打印结果 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com