SpringBoot教程(七)springboot配置WebMvc
阿新 • • 發佈:2018-12-26
一.配置springboot webmvc
1.springboot webmvc配置方式
通過繼承 extends WebMvcConfigurerAdapter
@Configuration
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
通過 implements WebMvcConfigurer
@Configuration public class SpringMvcConfig implements WebMvcConfigurer {
使用@EnableWebMvc 禁用預設配置 所有配置自己重寫
@Configuration
@EnableWebMvc
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
2.springboot為springmvc提供了預設的配置 主要包括檢視解析器、靜態資源處理、型別轉化器與格式化器、HTTP 訊息轉換器、靜態主頁的支援等。
3.EnableWebMvc註解問題
上面提到在使用 WebMvcConfigurer 擴充套件 Spring MVC 元件時, 類上不能加 @EnableWebMvc 註解,原因是在加上該註解後,Spring MVC 的自動配置會失效, 所有的東西都需要我們自動配置。
二.springboot webmvc配置講解
1.引數詳解
package com.spf.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.config.annotation.*; import java.util.List; @Configuration public class SpringMvcConfig extends WebMvcConfigurerAdapter { @Override public void configurePathMatch(PathMatchConfigurer configurer) { } @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { } @Override public void configureAsyncSupport(AsyncSupportConfigurer configurer) { } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { } @Override public void addFormatters(FormatterRegistry registry) { } @Override public void addInterceptors(InterceptorRegistry registry) { } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } @Override public void addCorsMappings(CorsRegistry registry) { } @Override public void addViewControllers(ViewControllerRegistry registry) { } @Override public void configureViewResolvers(ViewResolverRegistry registry) { } @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { } @Override public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) { } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { } @Override public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { } @Override public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) { } }