1 静态资源
如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:
spring.resources.static-locations=
classpath:/META-INF/resources/,
classpath:/resources/,
classpath:/static/,
classpath:/public/
能正常访问:
http://localhost:8080/public/01.png
2 消息转换器
Spring中默认的编码格式为ISO-8859-1,而SpringBoot帮助我们默认配置消息编码格式为UTF-8。
如果我们要自己自己配置的话,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。如下:
@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
类似于之前在配置文件中:
其他的比如JSON转换配置类似,创建一个对象放在容器中。
3 拦截器
在SpringBoot中的拦截器是继承自WebMvcConfigurerAdapter,重写addInterceptors方法,并且类上添加@Configuration,声明这是一个配置。
/**
* 拦截器配置
* @author Tang
*
*/
@Configuration//声明这是一个配置
public class MySpringMVConfig 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("/**");
}
}
需要说明的是,该类必须要和启动类放在同一个包下。
当启动类为HelloApplication时,将会扫描该包下所有类名标示为@Configuration的类型。当访问http://localhost:8080/hello时。
4 与传统的SSM项目进行对比,SpringBoot中需要配置的项。
部分摘自某智播客。