多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 1. Spring Boot的web开发 Web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration ### 1.1 自动配置的ViewResolver ![](https://box.kancloud.cn/874929102fe04ca311054a23492e4b1c_977x422.png) 视图的配置mvcProperties对象中: org.springframework.boot.autoconfigure.web.WebMvcProperties.View ![](https://box.kancloud.cn/59cca2f900beab7cd2c7e0b477c70646_614x294.png) ### 1.2 自动配置静态资源 1. 进入规则为 / 如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为: spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ ![](https://box.kancloud.cn/31369ef67442389c223961a6f917d058_800x206.png) 测试: ![](https://box.kancloud.cn/85f93f846de968e7f71d3a284f16f870_250x88.png) ![](https://box.kancloud.cn/2c6894eab9145fb29e658020b9b17afb_1058x703.png) ### 1.3 进入规则为*.xxx 或者 不指定静态文件路径时 将静态资源放置到webapp下的static目录中即可通过地址访问: ![](https://box.kancloud.cn/076d071c4bf08d171e90897ede5106fa_258x186.png) 测试: ### 1.4 自定义消息转化器 自定义消息转化器,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。 ~~~ ~~~ @Bean public StringHttpMessageConverter stringHttpMessageConverter(){ StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); return converter; ~~~ ~~~ 默认配置: ### 1.5 自定义SpringMVC的配置 有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。 ~~~ 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); } } ~~~ ## 2. 自定义 ![](https://box.kancloud.cn/acea9509726bd556cd807b071d7239e9_340x547.png) 1. HelloApplication ~~~ @Controller @SpringBootApplication(exclude = { RedisAutoConfiguration.class }) //@ComponentScan(basePackages={"cn.itcast.springboot.javaconfig"}) 默认扫描与应用同级的目录和这个目录的子目录,此时MySpringMVCConfig类配置,就不起作用了 @Configuration public class HelloApplication { @RequestMapping("hello") @ResponseBody public String hello() { return "hello world! 传智播客!"; } //自定义消息转换器 @Bean public StringHttpMessageConverter stringHttpMessageConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("ISO-8859-1")); return converter; } public static void main(String[] args) { // SpringApplication.run(HelloApplication.class, args); SpringApplication application = new SpringApplication(HelloApplication.class); application.setBannerMode(Mode.OFF); application.run(args); } } ~~~ 2. 自定义类 ~~~ @Configuration //申明这是一个配置,和HelloApplication在同一个目录下,所有配置会被扫描 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); } } ~~~