以下是Spring Boot中Spring MVC相关配置的详细描述及表格总结:
Spring MVC 配置项详解
1. 异步请求配置
spring.mvc.async.request-timeout 描述 :设置异步请求的超时时间(单位:毫秒)。默认值 :未设置(无限等待)。示例 :spring.mvc.async.request-timeout=30000(30秒超时)。
2. 内容协商配置
spring.mvc.contentnegotiation.favor-parameter 描述 :是否通过请求参数(默认参数名format)确定媒体类型。默认值 :false(不启用参数协商)。 spring.mvc.contentnegotiation.favor-path-extension 描述 :是否通过路径扩展(如.json)确定媒体类型。默认值 :false(不启用路径扩展协商)。 spring.mvc.contentnegotiation.media-types.* 描述 :定义媒体类型与文件扩展名的映射(如application/json对应.json)。示例 :spring.mvc.contentnegotiation.media-types.yml=application/yaml。 spring.mvc.contentnegotiation.parameter-name 描述 :自定义参数名(替代默认的format)。示例 :spring.mvc.contentnegotiation.parameter-name=media。
3. 日期格式配置
spring.mvc.format.date 描述 :全局日期格式(需与DateTimeFormatter兼容)。默认值 :无(使用Java默认格式)。示例 :spring.mvc.format.date=yyyy-MM-dd。
4. 请求处理配置
spring.mvc.dispatch-trace-request 描述 :是否启用对TRACE请求的处理。默认值 :false。 spring.mvc.dispatch-options-request 描述 :是否启用对OPTIONS请求的处理。默认值 :true。
5. 表单数据处理
spring.mvc.formcontent.filter.enabled 描述 :是否将PUT/PATCH的表单数据请求转换为POST。默认值 :true(启用转换)。
6. 模型与国际化
spring.mvc.ignore-default-model-on-redirect 描述 :重定向时是否忽略默认模型数据。默认值 :true(忽略默认模型)。 spring.web.locale 描述 :默认国际化语言(如zh_CN)。默认值 :由Accept-Language决定。 spring.web.locale-resolver 描述 :国际化解析策略(accept-header/fixed/header)。默认值 :accept-header(根据浏览器语言)。
7. 日志与异常
spring.mvc.log-resolved-exception 描述 :是否记录已处理异常的详细日志。默认值 :false(不记录)。
8. 路径匹配
spring.mvc.pathmatch.use-suffix-pattern 描述 :是否使用后缀模式匹配(如/api/*.*)。默认值 :false。
9. 视图与静态资源
spring.mvc.view.prefix/suffix 描述 :视图模板的前缀和后缀(如classpath:/templates/和.html)。默认值 :空字符串(需根据模板引擎配置)。 spring.mvc.static-path-pattern 描述 :静态资源路径匹配规则。默认值 :/**(匹配所有路径)。
10. 其他配置
spring.mvc.servlet.load-on-startup 描述 :Servlet启动时的加载优先级(-1表示延迟加载)。默认值 :-1。 spring.mvc.throw-exception-if-no-handler-found 描述 :无处理器时是否抛出NoHandlerFoundException。默认值 :false(返回404)。
Thymeleaf 配置项
配置项 描述 默认值 spring.thymeleaf.enabled是否启用Thymeleaf模板引擎 truespring.thymeleaf.prefix模板文件路径前缀(如classpath:/templates/) classpath:/templates/spring.thymeleaf.suffix模板文件后缀(如.html) .html
JSON配置(Jackson)
配置项 描述 默认值 spring.jackson.time-zoneJSON时间序列化的时区(如GMT+8) JVM默认时区 spring.jackson.date-format日期格式(如yyyy-MM-dd HH:mm:ss) yyyy-MM-dd HH:mm:ss Z
配置项总结表
配置项 描述 默认值 示例值 spring.mvc.async.request-timeout异步请求超时时间(毫秒) 无(无限) 30000spring.mvc.contentnegotiation.favor-parameter是否启用参数协商(如?format=json) falsetruespring.mvc.contentnegotiation.favor-path-extension是否启用路径扩展协商(如/api/data.json) falsetruespring.mvc.contentnegotiation.media-types.*媒体类型与扩展名映射(如yml=application/yaml) 无 spring.mvc.contentnegotiation.media-types.xml=application/xmlspring.mvc.contentnegotiation.parameter-name自定义参数名(替代format) formatmediaspring.mvc.format.date全局日期格式(如yyyy-MM-dd) 无 yyyy-MM-dd HH:mm:ssspring.mvc.dispatch-trace-request是否启用TRACE请求处理 falsetruespring.mvc.dispatch-options-request是否启用OPTIONS请求处理 truefalsespring.mvc.formcontent.filter.enabled是否将PUT/PATCH表单数据转换为POST truefalsespring.mvc.ignore-default-model-on-redirect重定向时是否忽略默认模型数据 truefalsespring.web.locale默认国际化语言(如zh_CN) 由Accept-Language决定 en_USspring.web.locale-resolver国际化解析策略(accept-header/fixed/header) accept-headerfixedspring.mvc.log-resolved-exception是否记录已处理异常日志 falsetruespring.mvc.pathmatch.use-suffix-pattern是否使用后缀模式匹配路径(如/api/*.*) falsetruespring.mvc.servlet.load-on-startupServlet加载优先级(-1为延迟加载) -11spring.mvc.static-path-pattern静态资源路径匹配规则(如/**) /**/static/**spring.mvc.throw-exception-if-no-handler-found无处理器时是否抛出异常 falsetruespring.mvc.view.prefix视图模板路径前缀(如classpath:/templates/) 空字符串 classpath:/views/spring.mvc.view.suffix视图模板后缀(如.html) 空字符串 .jsp
注意事项
Jackson配置 :spring.jackson.date-format需与Java的DateTimeFormatter兼容。Thymeleaf路径 :确保模板文件路径与prefix和suffix配置一致。国际化 :若需固定语言,设置spring.web.locale-resolver=fixed并指定spring.web.locale。