【发布时间】:2014-02-25 01:06:57
【问题描述】:
您好,我有一个 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;
}
我想看看运行它时 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
【问题讨论】:
标签: java json spring-mvc junit4