由於須要MockHttpServletResponse對象來獲得輸出的內容,要引入的包html
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.6.RELEASE</version> </dependency>
注入ViewResolver解析頁面spring
@Autowired private InternalResourceViewResolver viewResolver;
使用過程json
@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); }
查看打印和返回的結果,發現生成了htmlapp