SpringBoot--MVC相关配置

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中需要配置的项。
这里写图片描述
这里写图片描述

部分摘自某智播客。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值