View Resolvers

MVC 配置简化了视图解析器的注册。

下面的 Java 配置例子通过使用 JSP 和 Jackson 作为 JSON 渲染的默认视图,配置了内容协商视图解析:

  1. @Configuration
  2. @EnableWebMvc
  3. public class WebConfig implements WebMvcConfigurer {
  4. @Override
  5. public void configureViewResolvers(ViewResolverRegistry registry) {
  6. registry.enableContentNegotiation(new MappingJackson2JsonView());
  7. registry.jsp();
  8. }
  9. }

对应的 XML

  1. <mvc:view-resolvers>
  2. <mvc:content-negotiation>
  3. <mvc:default-views>
  4. <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
  5. </mvc:default-views>
  6. </mvc:content-negotiation>
  7. <mvc:jsp/>
  8. </mvc:view-resolvers>

然而,请注意,FreeMarker、Tiles、Groovy Markup 和 script templates(脚本模板)也需要配置底层视图技术。

MVC 命名空间提供了专门的元素。下面的例子适用于 FreeMarker:

  1. <mvc:view-resolvers>
  2. <mvc:content-negotiation>
  3. <mvc:default-views>
  4. <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
  5. </mvc:default-views>
  6. </mvc:content-negotiation>
  7. <mvc:freemarker cache="false"/>
  8. </mvc:view-resolvers>
  9. <mvc:freemarker-configurer>
  10. <mvc:template-loader-path location="/freemarker"/>
  11. </mvc:freemarker-configurer>

对应的 Java 配置

  1. @Configuration
  2. @EnableWebMvc
  3. public class WebConfig implements WebMvcConfigurer {
  4. @Override
  5. public void configureViewResolvers(ViewResolverRegistry registry) {
  6. registry.enableContentNegotiation(new MappingJackson2JsonView());
  7. registry.freeMarker().cache(false);
  8. }
  9. @Bean
  10. public FreeMarkerConfigurer freeMarkerConfigurer() {
  11. FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
  12. configurer.setTemplateLoaderPath("/freemarker");
  13. return configurer;
  14. }
  15. }

例子

这个内容协商很强悍:客户端需要什么格式,就给你返回什么格式

  1. package cn.mrcode.study;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  5. import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
  8. /**
  9. * @author mrcode
  10. */
  11. @EnableWebMvc
  12. @ComponentScan("cn.mrcode.study.springdocsread")
  13. @Configuration
  14. public class AppConfig implements WebMvcConfigurer {
  15. @Override
  16. public void configureViewResolvers(ViewResolverRegistry registry) {
  17. registry.enableContentNegotiation(new MappingJackson2JsonView());
  18. }
  19. }

然后有一个控制器

  1. package cn.mrcode.study.springdocsread.web;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestBody;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import java.util.Date;
  9. import javax.validation.Valid;
  10. /**
  11. * @author mrcode
  12. */
  13. @Controller
  14. @RequestMapping("/demo")
  15. public class Demo2Controller {
  16. @GetMapping("/accept")
  17. @ResponseBody
  18. public Pet data2() {
  19. final Pet pet = new Pet();
  20. pet.setDate(new Date());
  21. pet.setName("张三");
  22. return pet;
  23. }
  24. }

在请求的时候,就可以通过请求头 Accept 来让返回的 Pet 格式化成你这里想要的格式,比如

  1. ### 默认情况
  2. GET http://localhost:8080/demo/accept
  3. // 响应信息
  4. HTTP/1.1 200
  5. Vary: Origin
  6. Vary: Access-Control-Request-Method
  7. Vary: Access-Control-Request-Headers
  8. Content-Type: application/xml;charset=UTF-8
  9. Transfer-Encoding: chunked
  10. Date: Mon, 11 Apr 2022 07:39:41 GMT
  11. Keep-Alive: timeout=60
  12. Connection: keep-alive
  13. <Pet>
  14. <name>张三</name>
  15. <date>1649662781992</date>
  16. </Pet>
  1. ### 可以要求想要 json 格式数据
  2. GET http://localhost:8080/demo/accept
  3. Accept: application/json
  4. //
  5. HTTP/1.1 200
  6. Vary: Origin
  7. Vary: Access-Control-Request-Method
  8. Vary: Access-Control-Request-Headers
  9. Content-Type: application/json
  10. Transfer-Encoding: chunked
  11. Date: Mon, 11 Apr 2022 07:43:19 GMT
  12. Keep-Alive: timeout=60
  13. Connection: keep-alive
  14. {
  15. "name": "张三",
  16. "date": 1649662999369
  17. }