因为需要MockHttpServletResponse对象来得到输出的内容,要引入的包
org.springframework spring-test 5.1.6.RELEASE
注入ViewResolver解析页面
@Autowired private InternalResourceViewResolver viewResolver;
使用过程
@GetMapping("test") public JsonResult test(HttpServletRequest request, HttpServletResponse response)throws Exception{ ModelMap map = new ModelMap(); map.put("param_json", "asdasd"); map.put("loginuser", null); View resolve = viewResolver.resolveViewName("design", Locale.CHINA); MockHttpServletResponse mockResp = new MockHttpServletResponse(); resolve.render(map, request, mockResp); System.out.println("rendered html : " + mockResp.getContentAsString()); return JsonResult.ok(mockResp.getContentAsString(), null); }
查看打印和返回的结果,发现生成了html