MVC 配置简化了视图解析器的注册。
下面的 Java 配置例子通过使用 JSP 和 Jackson 作为 JSON 渲染的默认视图,配置了内容协商视图解析:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.enableContentNegotiation(new MappingJackson2JsonView());
registry.jsp();
}
}
对应的 XML
<mvc:view-resolvers>
<mvc:content-negotiation>
<mvc:default-views>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</mvc:default-views>
</mvc:content-negotiation>
<mvc:jsp/>
</mvc:view-resolvers>
然而,请注意,FreeMarker、Tiles、Groovy Markup 和 script templates(脚本模板)也需要配置底层视图技术。
MVC 命名空间提供了专门的元素。下面的例子适用于 FreeMarker:
<mvc:view-resolvers>
<mvc:content-negotiation>
<mvc:default-views>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</mvc:default-views>
</mvc:content-negotiation>
<mvc:freemarker cache="false"/>
</mvc:view-resolvers>
<mvc:freemarker-configurer>
<mvc:template-loader-path location="/freemarker"/>
</mvc:freemarker-configurer>
对应的 Java 配置
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.enableContentNegotiation(new MappingJackson2JsonView());
registry.freeMarker().cache(false);
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/freemarker");
return configurer;
}
}
例子
这个内容协商很强悍:客户端需要什么格式,就给你返回什么格式
package cn.mrcode.study;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
/**
* @author mrcode
*/
@EnableWebMvc
@ComponentScan("cn.mrcode.study.springdocsread")
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.enableContentNegotiation(new MappingJackson2JsonView());
}
}
然后有一个控制器
package cn.mrcode.study.springdocsread.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import javax.validation.Valid;
/**
* @author mrcode
*/
@Controller
@RequestMapping("/demo")
public class Demo2Controller {
@GetMapping("/accept")
@ResponseBody
public Pet data2() {
final Pet pet = new Pet();
pet.setDate(new Date());
pet.setName("张三");
return pet;
}
}
在请求的时候,就可以通过请求头 Accept 来让返回的 Pet 格式化成你这里想要的格式,比如
### 默认情况
GET http://localhost:8080/demo/accept
// 响应信息
HTTP/1.1 200
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 11 Apr 2022 07:39:41 GMT
Keep-Alive: timeout=60
Connection: keep-alive
<Pet>
<name>张三</name>
<date>1649662781992</date>
</Pet>
### 可以要求想要 json 格式数据
GET http://localhost:8080/demo/accept
Accept: application/json
//
HTTP/1.1 200
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 11 Apr 2022 07:43:19 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"name": "张三",
"date": 1649662999369
}