Spring MVC 4.2.4.RELEASE 中文文档
  • Introduction
  • Part I: 目录与翻译注记(Table of contents and Notes)
    • 目录
    • 翻译注记
  • Part II: 文档内容(Documentation contents)
    • Spring Web MVC框架简介
      • Spring Web MVC的新特性
      • 允许其他MVC实现
    • DispatcherServlet
      • WebApplicationContext中特殊的Bean类型
      • 默认的DispatcherServlet配置
      • DispatcherServlet的处理流程
    • 控制器的实现
      • 使用@Controller注解定义一个控制器
      • 使用@RequestMapping注解映射请求路径
      • 定义@RequestMapping注解的处理方法
      • 异步请求的处理
      • 对控制器测试
    • 处理器映射
      • 使用HandlerInterceptor拦截请求
    • 视图解析
      • 使用ViewResolver接口解析视图
      • 视图链
      • 视图重定向
      • 内容协商解析器ContentNegotiatingViewResolver
    • 使用闪存属性FlashAttributes
    • URI构造
      • 为控制器和方法指定URI
      • 在视图中为控制器和方法指定URI
    • 地区信息
      • 获取时区信息
      • Accept请求头解析器AcceptHeaderLocaleResolver
      • Cookie解析器CookieLocaleResolver
      • Session解析器SessionLocaleResolver
      • 地区更改拦截器LocaleChangeInterceptor
    • 主题 themes
      • 关于主题:概览
      • 定义主题
      • 主题解析器
    • Spring的multipart(文件上传)支持
      • 概述
      • 使用MultipartResolver与Commons FileUpload传输文件
      • Servlet 3.0下的MultipartResolver
      • 处理表单中的文件上传
      • 处理客户端发起的文件上传请求
    • 异常处理
      • 处理器异常解析器HandlerExceptionHandler
      • @ExceptionHandler注解
      • 处理一般的Spring MVC异常
      • 使用@ResponseStatus注解业务异常
      • Servlet默认容器错误页面的定制化
    • Web安全
    • "约定优于配置"的支持
      • 控制器类名-处理器映射ControllerClassNameHandlerMapping
      • 模型ModelMap(ModelAndView)
      • 视图-请求与视图名的映射
    • HTTP缓存支持
      • HTTP请求头Cache-Control
      • 对静态资源的HTTP缓存支持
      • 在控制器中设置Cache-Control、ETag和Last-Modified响应头
      • 弱ETag
    • 基于代码的Servlet容器初始化
    • 配置Spring MVC
      • 启用MVC Java编程配置或MVC命名空间
      • 默认配置的定制化
      • 转换与格式化
      • 验证
      • 拦截器
      • 内容协商
      • 视图控制器
      • 视图解析器
      • 资源的服务
      • 回到默认的Servlet来进行资源服务
      • 路径匹配
      • 消息转换器
      • 使用MVC Java编程进行高级定制
      • 使用MVC命名空间进行高级定制
Powered by GitBook
On this page

Was this helpful?

  1. Part II: 文档内容(Documentation contents)
  2. 配置Spring MVC

内容协商

Previous拦截器Next视图控制器

Last updated 5 years ago

Was this helpful?

You can configure how Spring MVC determines the requested media types from the request. The available options are to check the URL path for a file extension, check the "Accept" header, a specific query parameter, or to fall back on a default content type when nothing is requested. By default the path extension in the request URI is checked first and the "Accept" header is checked second.

The MVC Java config and the MVC namespace register json, xml, rss, atom by default if corresponding dependencies are on the classpath. Additional path extension-to-media type mappings may also be registered explicitly and that also has the effect of whitelisting them as safe extensions for the purpose of RFD attack detection (see for more detail).

Below is an example of customizing content negotiation options through the MVC Java config:

_@Configuration_
_@EnableWebMvc_
public class WebConfig extends WebMvcConfigurerAdapter {

    _@Override_
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.mediaType("json", MediaType.APPLICATION_JSON);
    }
}

In the MVC namespace, the <mvc:annotation-driven> element has a content- negotiation-manager attribute, which expects a ContentNegotiationManager that in turn can be created with a ContentNegotiationManagerFactoryBean:

json=application/json xml=application/xml

If not using the MVC Java config or the MVC namespace, you'll need to create an instance of ContentNegotiationManager and use it to configure RequestMappingHandlerMapping for request mapping purposes, and RequestMappingHandlerAdapter and ExceptionHandlerExceptionResolver for content negotiation purposes.

Note that ContentNegotiatingViewResolver now can also be configured with a ContentNegotiationManager, so you can use one shared instance throughout Spring MVC.

In more advanced cases, it may be useful to configure multiple ContentNegotiationManager instances that in turn may contain custom ContentNegotiationStrategy implementations. For example you could configure ExceptionHandlerExceptionResolver with a ContentNegotiationManager that always resolves the requested media type to "application/json". Or you may want to plug a custom strategy that has some logic to select a default content type (e.g. either XML or JSON) if no content types were requested.

the section called "Suffix Pattern Matching and RFD"