* 包含`ContentNegotiatingViewResolver`和`BeanNameViewResolver `bean。
* 支持提供靜態資源,包括對WebJars的支持(參考27.1.5))。
* 自動注冊`Converter`,`GenericConverter`和`Formatter` bean。
* 支持`HttpMessageConverters`(參考27.1.2)。
* 自動注冊`MessageCodesResolver`(參考27.1.4)。
* 靜態`index.html`支持。
* 自定義`Favicon`支持(參考27.1.7)。
* 自動使用`ConfigurableWebBindingInitializer` bean(參考27.1.9)。
如果你想保持 Spring Boot MVC的特點,并添加mvc的其他配置(interceptors, formatters, view controllers),添加`@Configuration`到類`WebMvcConfigurer`,不要使用`@EnableWebMvc`.,如下:
```
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.ixinnuo.financial.framework.LoginInterceptor;
/**
* web相關配置
*
* @author liqq
*
*/
@Configuration
public class ClientWebAppConfigurer implements WebMvcConfigurer {
@Autowired
LoginInterceptor loginInterceptor;
/**
* 注冊資源,放行
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// swagger靜態資源及對應的訪問路徑
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
//jar包內的靜態資源
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
// 項目的靜態資源及對應的訪問路徑
,默認是/**,一旦自定義攔截器就無法再使用默認的url了
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
/**
* 注冊登錄攔截器,放行登錄和注冊兩個接口
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/", // 首頁
"/error", // 錯誤頁
"/swagger-resources/**", "/swagger-ui.html/**", // swagger
"/kaptcha/**", // 圖形驗證碼
"/sms/**", // 短信驗證碼
"/user/login", // 登錄
"/user/register", // 注冊
"/sysOperationLog/**", // 注冊
"/static/**","/webjars/**");//靜態資源
WebMvcConfigurer.super.addInterceptors(registry);
}
/**
* 添加日期類型轉換器
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToDateConverter());
WebMvcConfigurer.super.addFormatters(registry);
}
}
```
下面是用到的日期轉換類
```
package com.ixinnuo.financial.conf;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.converter.Converter;
/**
* 日期轉換器,解決在post請求中日期類型參數自動轉Date類型
*
*/
public class StringToDateConverter implements Converter<String, Date> {
private static final String dateFormatFirst = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormatSecond = "yyyy-MM-dd";
private static final String dateFormatThird = "yyyyMMdd";
@Override
public Date convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
source = source.trim();
SimpleDateFormat formatter;
try {
if(source.contains(":")){
formatter = new SimpleDateFormat(dateFormatFirst);
}else if(source.contains("-")){
formatter = new SimpleDateFormat(dateFormatSecond);
}else{
formatter = new SimpleDateFormat(dateFormatThird);
}
Date dtDate = formatter.parse(source);
return dtDate;
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
}
}
```
如果想提供自定義實例`RequestMappingHandlerMapping`, `RequestMappingHandlerAdapter`, 或`ExceptionHandlerExceptionResolver`,你可以聲明`WebMvcRegistrationsAdapter `
如果想完全控制mvc的配置,使用`@EnableWebMvc`和` @Configuration`一起.
- I. Spring Boot Documentation
- 1.關于文檔
- 2. 獲取幫助
- 3.第一步
- 4.使用spring boot
- 5.學習spring boot的特點
- 6.轉移到生產
- 7.高級話題
- II. Getting Started
- 8.引入spring boot
- 9.系統需要
- 9.1 Servlet容器
- 10.安裝spring boot
- 10.1 java開發者安裝說明
- 10.1.1 Maven Installation
- 10.2 Installing the Spring Boot CLI TODO
- 10.3 從早期的版本升級
- 11.開發你的第一個spring boot應用
- 11.1 創建POM
- 11.2 添加Classpath依賴
- 11.3 寫代碼
- 11.3.1 @RestController 和@RequestMapping 注解
- 11.3.2 @EnableAutoConfiguration注解
- 11.3.3 main方法
- 11.4運行 Example
- 11.5 創建可執行的Jar
- 12.下一步要閱讀的
- III. Using Spring Boot
- 13.構建系統
- 13.1 依賴管理
- 13.2 Maven
- 13.2.1 繼承Starter Parent
- 13.2.2 不使用Spring Boot的Parent POM
- 13.2.3 使用Spring Boot Maven 插件
- 13.3. Gradle TODO
- 13.4. Ant TODO
- 13.5. Starters TODO
- 14.結構化代碼
- 14.1使用“default”包
- 14.2 主應用程序類的位置
- 15.配置類
- 15.1 引入新增的配置類
- 15.2 引入xml配置
- 16.自動化配置
- 16.1 逐漸替換自動配置
- 16.2 禁用特定的配置類
- 17.spring的bean和依賴配置
- 18.使用@SpringBootApplication注解
- 19.運行的你的程序
- 19.1 在IDE中運行
- 19.2 運行打包的jar
- 19.3 使用maven插件運行
- 19.4 使用Gradle 插件 TODO
- 19.5 熱交換
- 20.開發者工具
- 20.1 默認屬性
- 20.2 自動重啟
- 20.2.1 記錄環境評估的更改
- 20.2.2 排除Resources
- 20.2.3 監視其他路徑
- 20.2.4 禁止重啟
- 20.2.5 使用觸發文件
- 20.2.6 自定義重啟類加載器
- 20.2.7 了解限制
- 20.3 實時加載
- 20.4 全局設置
- 20.5 遠程應用
- 20.5.1 運行遠程客戶端
- 20.5.2 遠程更新
- 21.把你的應用打包到生產
- 22.下一步要閱讀的
- IV. Spring Boot features
- 23.spring應用
- 23.1 啟動失敗
- 23.10 管理功能
- 23.8 使用ApplicationRunner 或 CommandLineRunner
- 24.可擴展的配置
- 24.1 配置隨機值
- 24.2 訪問命令行屬性
- 24.3 配置文件
- 24.4 特定配置文件
- 24.5 屬性的占位符
- 24.6 使用yaml替代properties
- 24.7 類型安全的配置屬性
- 24.7.1 第三方配置
- 24.7.2 靈活的綁定
- 25.外部的配置profiles
- 25.1 追加可用的外部配置
- 25.2 編程式的設置外部配置
- 25.3 外部配置文件
- 26.日志
- 26.1 日志格式
- 26.2 控制臺輸出
- 26.2.1顏色代碼數據
- 26.3 文件輸出
- 26.4 日志級別
- 26.5 自定義日志配置
- 26.6 Logback擴展
- 26.6.1特定于配置文件的配置
- 26.6.2 環境屬性
- 26.6.3 logback.xml配置說明
- 27.開發web應用
- 27.1 spring mvc
- 27.1.1 Spring MVC自動配置
- 27.1.2 HttpMessageConverters
- 27.1.3 定制JSON序列號和反序列化
- 27.1.4 MessageCodesResolver
- 27.1.5 靜態內容
- 27.1.6 歡迎頁面
- 27.1.7 定制Favicon
- 27.1.8 路徑匹配和內容判斷
- 27.1.9 ConfigurableWebBindingInitializer
- 27.1.10 模板引擎
- 27.1.11 處理錯誤
- 27.1.12 Spring HATEOAS
- 27.1.13 跨域支持
- 27.2 spring webflux
- 28.安全
- 28.1 mvc安全
- httpSecurity,webSecurity,authenticationManager
- 29.使用SQL數據庫
- 29.1 配置數據源
- 29.1.1 嵌入式數據庫支持
- 29.1.2 可用于生產的數據庫
- 29.1.3 從JNDI DataSource獲取連接
- 30.使用NoSql技術
- 30.1 Redis
- 30.1.1 連接Redis
- 31.緩存
- 32.消息
- 32.1 jms
- 32.2 amqp
- 32.2.1 支持RabbitMQ
- 32.2.2 發送消息
- 32.2.3 接收消息
- 33.使用RestTemplate調用REST服務
- 34.使用WebClient調用REST 服務
- 35.驗證
- 36.發郵件
- 37.使用JTA的分布式事務
- 37.01 結合spring框架的本地事務
- 38. Hazelcast
- 39.定時任務
- 40.spring集成
- 41.spring會話
- 42.JMX的監控和管理
- 43.測試
- 43.1測試范圍的依賴
- 43.2 測試Spring應用
- 43.2 測試spring boot應用
- 44. WebSockets
- 45. Web Services
- 46.創建你自己的自動化配置
- 47. Kotlin的支持
- 48.下一步要閱讀的
- V. Spring Boot Actuator: Production-ready features
- 49.開啟生產用的特性
- 50.端點
- 50.1 啟動端點
- 50.2 暴露端點
- 50.3 安全的http端點
- 51.HTTP的監控和管理
- 52.JMX的監控和管理
- 53.日志
- 54.度量
- 55.審計
- 56.HTTP追蹤
- 57.過程監控
- 58.上云的支持
- 59.下一步要閱讀的
- VI. Deploying Spring Boot Applications
- 60.云端的部署
- 61.安裝spring boot應用
- 62.下一步要閱讀的
- VII. Spring Boot CLI
- 63.安裝CTL
- 64.使用CTL
- 65.使用Groovy Beans DSL開發應用
- 66.使用settings.xml配置CTL
- 67.下一步要閱讀的
- VIII. Build tool plugins
- 68. Spring Boot Maven插件
- 69. Spring Boot Gradle插件
- 70. Spring Boot AntLib模塊
- 71.其他構建系統的支持
- 72.下一步要閱讀的
- IX. ‘How-to’ guides
- 73.spring boot應用
- 74.屬性和配置
- 75.嵌入式的web服務器
- 76. Spring MVC
- 76.1 寫JSON風格的服務
- 76.2 寫XML風格的服務
- 76.3 定制Jackson ObjectMapper
- 76.4 定制@ResponseBody呈現
- 76.5 處理文件上傳
- 77. Jersey
- 78. HTTP Clients
- 79. 日志
- 80. 數據訪問
- 80.1 配置自己的數據源
- 81. 數據庫初始化
- 82. 消息
- 83. 批處理應用
- 84. Actuator
- 85. 安全
- 86. 熱交換
- 87.構建
- 88.傳統開發
- X. Appendices
- A.通用的配置
- B. 配置元信息
- C.自動配置類
- D. 測試自動配置注解
- E. 可執行的jar格式
- F. 依賴的版本