以下是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模板引擎 | true |
spring.thymeleaf.prefix | 模板文件路径前缀(如classpath:/templates/ ) | classpath:/templates/ |
spring.thymeleaf.suffix | 模板文件后缀(如.html ) | .html |
JSON配置(Jackson)
配置项 | 描述 | 默认值 |
---|
spring.jackson.time-zone | JSON时间序列化的时区(如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 | 异步请求超时时间(毫秒) | 无(无限) | 30000 |
spring.mvc.contentnegotiation.favor-parameter | 是否启用参数协商(如?format=json ) | false | true |
spring.mvc.contentnegotiation.favor-path-extension | 是否启用路径扩展协商(如/api/data.json ) | false | true |
spring.mvc.contentnegotiation.media-types.* | 媒体类型与扩展名映射(如yml=application/yaml ) | 无 | spring.mvc.contentnegotiation.media-types.xml=application/xml |
spring.mvc.contentnegotiation.parameter-name | 自定义参数名(替代format ) | format | media |
spring.mvc.format.date | 全局日期格式(如yyyy-MM-dd ) | 无 | yyyy-MM-dd HH:mm:ss |
spring.mvc.dispatch-trace-request | 是否启用TRACE 请求处理 | false | true |
spring.mvc.dispatch-options-request | 是否启用OPTIONS 请求处理 | true | false |
spring.mvc.formcontent.filter.enabled | 是否将PUT/PATCH 表单数据转换为POST | true | false |
spring.mvc.ignore-default-model-on-redirect | 重定向时是否忽略默认模型数据 | true | false |
spring.web.locale | 默认国际化语言(如zh_CN ) | 由Accept-Language 决定 | en_US |
spring.web.locale-resolver | 国际化解析策略(accept-header /fixed /header ) | accept-header | fixed |
spring.mvc.log-resolved-exception | 是否记录已处理异常日志 | false | true |
spring.mvc.pathmatch.use-suffix-pattern | 是否使用后缀模式匹配路径(如/api/*.* ) | false | true |
spring.mvc.servlet.load-on-startup | Servlet加载优先级(-1为延迟加载) | -1 | 1 |
spring.mvc.static-path-pattern | 静态资源路径匹配规则(如/** ) | /** | /static/** |
spring.mvc.throw-exception-if-no-handler-found | 无处理器时是否抛出异常 | false | true |
spring.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
。