Springboot中WebMvcConfigurer接口详解

Springboot 使用越来越多,企业的基本框架,到Springcloud分布式,可以说无论面试还是平常技术学习,一说到spring几乎就就代替了Java,可以说spring,springboot的力量之强大;

今天的主角是WebMvcConfigurer :

  这个接口很重要,如果一个项目没有拦截器,想想就可怕,小编也是遇到过类似的问题:

  这个接口主要功能如下:

     addInterceptors:拦截器

     addViewControllers:页面跳转

     addResourceHandlers:静态资源

    configureDefaultServletHandling:默认静态资源处理器

    configureViewResolvers:视图解析器

    configureContentNegotiation:配置内容裁决的一些参数

    addCorsMappings:跨域

    configureMessageConverters:信息转换器

    要记住你学到的每个接口的名称,因为别人问你,能够准确说出名字也很重要,本人觉得这个接口的重点是:跨域,拦截器,静态资源处理器,信息转化器,最好能记住;

   在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等。SpringBoot 2.0 后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一实现     WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类,具体实现可看这篇文章。https://blog.csdn.net/fmwind/article/details/82832758

  写本文时springboot最新版本是2.2.5  官网:https://spring.io/projects/spring-boot

   例子:

package com.olive.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* WebMvcConfigurer
*
*/
@Configuration
@EnableWebMvc
public class ConfigurerAdapter implements WebMvcConfigurer {

//图片保存路径
public static final String PIC_PATH = "/landscape/";
@Value(value="${application.profile}")
private String profile;

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("GET","POST","PUT","DELETE");

}

// 可解决Long 类型在 前端精度丢失的问题, 如不想全局 直接添加注解 @JsonSerialize(using= ToStringSerializer.class) 到相应的字段

// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//
// MappingJackson2HttpMessageConverter jackson2HttpMessageConverter =
// new MappingJackson2HttpMessageConverter();
//
// ObjectMapper objectMapper = new ObjectMapper();
// SimpleModule simpleModule = new SimpleModule();
// simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
// simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
// simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
// objectMapper.registerModule(simpleModule);
// jackson2HttpMessageConverter.setObjectMapper(objectMapper);
// converters.add(jackson2HttpMessageConverter);
// converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
// }

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
/** 图片传路径 */
registry.addResourceHandler("/landscape/**").addResourceLocations("file:" + profile);
}
}

这个类实现了WebMvcConfigurer 接口,这个类是我在搭建框架的时候,直接运用在生产的所以可以直接copy走;先到这结束。。。。
原文地址:https://www.cnblogs.com/liuys635/p/13456261.html