ModelAndView的内容类型

时间:2015-04-28 08:18:15

标签: spring-mvc junit

这是我的功能

@RequestMapping(value = { "/showTaskMonitorStatus.do", "/showStatusPage.do" },                                                                                                                                                     method = RequestMethod.GET)   
public ModelAndView handleShowTaskMonitorStatus(HttpServletRequest request) {
    ModelAndView model = new ModelAndView();
    -----------------------------------
    return model;
}

我的测试用例看起来像,

    @Test
    public void testHandleShowTaskMonitorStatus() throws Exception {
    MvcResult result =  mockMvc.perform(get("/showTaskMonitorStatus.do"))
    .andDo(MockMvcResultHandlers.print()) // Print request and response
    .andExpect(status().isOk())
    .andExpect(content().contentType("application/json")) //but in this line I am getting error.(Content type not set.)
    .andReturn(); 
    --------------------------------------
    }

任何人都可以建议我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以像这样修改@RequestMapping以设置正确的内容类型:

@RequestMapping(value = { ... }, produces = MediaType.APPLICATION_JSON_VALUE)