大概了解三种方式:
1.区别在于WebMvcConfigurerAdapter 新版本过期,不建议使用。
2.剩下两种不建议同时使用,WebMvcConfigurationSupport是webmvc的配置类,如果在springboot项目中,有类继承了WebMvcConfigurationSupport,那么webmvc的自动配置类WebMvcAutoConfiguration就会失效,导致挂载页面访问不到。
3.如果继承WebMvcConfigurer重写configureMessageConverters但没生效,转换器是列表添加的形式,add进去后就是最后一个,所以没有生效。添加到第一位即可
converters.add(converter);改为converters.add(0,converter);
1.继承WebMvcConfigurerAdapter
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ming.core.utils.SpringBeanManagerUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;/*** 拦截器配置*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void extendMessageConverters(List> converters) {FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();converters.add(fastJsonHttpMessageConverter); }
}
2.实现WebMvcConfigurer接口
@EnableWebMvc
@RequiredArgsConstructor
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {private final RequestIntercept requestIntercept;@Overridepublic void configureMessageConverters(List> converters) {// 1.需要定义一个convert转换消息的对象;FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();// 2:添加fastJson的配置信息;FastJsonConfig fastJsonConfig = new FastJsonConfig();// 把值是空的直接过滤掉,去掉循环转换fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullBooleanAsFalse);// 4.在convert中添加配置信息.fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);HttpMessageConverter> converter = fastJsonHttpMessageConverter;converters.add(converter);}}
3.继承WebMvcConfigurationSupport
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;/*** @CreateTime: 2023-03-20 16:10* @Description: TODO*/
@Configuration
public class JsonMessageConfig extends WebMvcConfigurationSupport {/*** 使用阿里 fastjson 作为JSON MessageConverter* @param converters*/@Overridepublic void configureMessageConverters(List> converters) {FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();FastJsonConfig config = new FastJsonConfig();config.setSerializerFeatures(// 保留map空的字段SerializerFeature.WriteMapNullValue,// 将String类型的null转成""SerializerFeature.WriteNullStringAsEmpty,// 将Number类型的null转成0SerializerFeature.WriteNullNumberAsZero,// 将List类型的null转成[]SerializerFeature.WriteNullListAsEmpty,// 将Boolean类型的null转成falseSerializerFeature.WriteNullBooleanAsFalse,// 避免循环引用SerializerFeature.DisableCircularReferenceDetect);converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);List mediaTypeList = new ArrayList<>();// 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"mediaTypeList.add(MediaType.APPLICATION_JSON);converter.setSupportedMediaTypes(mediaTypeList);converters.add(converter);}
}
WebMvcConfigurationSupport
和WebMvcConfigurer
都是Spring MVC框架中用于配置WebMvc的接口/类,但是它们有一些不同之处:
WebMvcConfigurationSupport
是一个类,而WebMvcConfigurer
是一个接口。WebMvcConfigurationSupport
提供了一个基本的WebMvc配置类,您可以继承该类并覆盖它的方法来自定义您的配置。WebMvcConfigurer
则是一个接口,它定义了一系列回调方法,您可以在您的应用程序中实现这些方法以自定义Spring MVC的配置。WebMvcConfigurationSupport
可以用来配置全局的WebMvc配置,例如ViewResolver
和ArgumentResolver
等。您可以使用@EnableWebMvc
注解来启用它。而WebMvcConfigurer
则可以用于配置局部的WebMvc配置,例如添加拦截器、解析器等。您可以使用@Configuration
注解配合@EnableWebMvc
注解来启用它。WebMvcConfigurationSupport
可以使用@Autowired注解来注入其他的Bean,例如ConversionService
和Validator
等。而WebMvcConfigurer
则不能使用@Autowired注解,因为它是一个接口。 总的来说,WebMvcConfigurationSupport
提供了更全面的WebMvc配置,但是它也更为复杂,适用于全局的配置。而WebMvcConfigurer
则提供了更为灵活的配置方式,适用于局部的配置。
上一篇:文件包含漏洞全面详解
下一篇:链表(没做完。。)