SpringBoot学习<五>(SpringMvc自动配置)

Spring MVC

Spring Web MVC框架(通常简称为springmvc)是一个“model view controller“的Web框架。Spring MVC允许您创建@Controller或@RestController的bean来处理传入的HTTP请求。控制器中的方法使用@RequestMapping注释映射到HTTP。

下面是@RestController提供JSON数据的典型示例:

<code>@RestController
@RequestMapping(value="/users")
public class MyRestController {

@RequestMapping(value="/{user}", method=RequestMethod.GET)
public User getUser(@PathVariable Long user) {
// ...
}

@RequestMapping(value="/{user}/customers", method=RequestMethod.GET)
List<customer> getUserCustomers(@PathVariable Long user) {
// ...
}

@RequestMapping(value="/{user}", method=RequestMethod.DELETE)
public User deleteUser(@PathVariable Long user) {
// ...
}

}/<customer>/<code>

Spring Boot对SpringMvc的默认配置

SpringBoot 的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

SpringBoot 自动配置好了SpringMVC,以下是SpringBoot对SpringMVC的默认配置:

1.自动配置了ContentNegotingViewResolver和BeanNameViewResolver bean。ContentNegotiatingViewResolver:组合所有的视图解析器。

SpringBoot学习<五>(SpringMvc自动配置)

2.支持服务静态资源,包括支持WebJars。如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

SpringBoot学习<五>(SpringMvc自动配置)


SpringBoot学习<五>(SpringMvc自动配置)

SpringBoot学习<五>(SpringMvc自动配置)

进入规则为*.xxx 或者 不指定静态文件路径时


SpringBoot学习<五>(SpringMvc自动配置)


SpringBoot学习<五>(SpringMvc自动配置)

3.Converter,GenericConverter,Formatter。Converter(转换器):将页面提交的数据转换为后台接受的数据(如页面提交用户的年龄,性别信息,后台用User对象接收,使用Converter做类型转换),Formatter格式化器(页面传入2017.12.17转为Date类型)。

4.支持HttpMessageConverter。Spring MVC使用HttpMessageConverter接口来转换HTTP请求和响应。可以将对象可以自动转换为JSON(使用Jackson库)或XML。默认情况下,字符串使用UTF-8编码。

SpringBoot学习<五>(SpringMvc自动配置)

SpringBoot 中自定义消息转换器:

<code>  @Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
} /<code>

默认配置:

SpringBoot学习<五>(SpringMvc自动配置)

SpringBoot学习<五>(SpringMvc自动配置)

5.自动注册MessageCodesResolver。定义错误代码生成规则。

6.静态首页访问,index.html。

7.自定义Favicon支持。

8.自动使用可配置的WebBindingInitializer bean。

自定义配置SpringMVC

有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。

<code>import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration //申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{

// 自定义拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("自定义拦截器............");

return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {

}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
}
};
registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
}

// 自定义消息转化器的第二种方法
@Override
public void configureMessageConverters(List<httpmessageconverter>> converters) {
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
converters.add(converter);
}

}/<httpmessageconverter>/<code>

如果您想保留SpringBoot MVC特性,并且只想添加额外的MVC配置(拦截器、Formatter、视图控制器等),那么可以编写一个配置类(@Confifiguration),是WebMvcConfigurerAdapter类型,但不需要@EnableWebMvc。如果希望提供RequestMappingHandlerMapping、questMappingHandlerAdapter或ExceptionHandlerExceptionResolver的自定义实例,则可以声明提供此类组件的WebMVCReristrationAdapter实例。

全面接管SpringMVC

SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了,我们需要在配置类中添加@EnableWebMvc即可;

<code>//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /test 请求来到 success
registry.addViewController("/test").setViewName("success");
}
}
/<code>

1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、 @Component)如 果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默 认的组合起来;

2)、在SpringBoot中会有非常多的xxxConfifigurer帮助我们进行扩展配置

3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置


分享到:


相關文章: