spring 2.X版本后需要实现WebMvcConfig
WebMvcConfig内容,要实现WebMvcConfigurer
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author
* @Desc mvc配置
*/
@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {
// 自定义拦截器
@Autowired
private AuthInterceptor authInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor).addPathPatterns("/bbs/**")
.excludePathPatterns("/api/v1/healthCheck");
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
}
/**
* fastJson 消息转换器
*
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
/// -> JSON消息转换器(采用阿里的fastjson)
// 创建一个转换器对象;
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// 个性化配置转换特性
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 配置:要格式化返回的json数据
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 配置:把空的值的key也返回
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
// 字段如果为null,输出为false,而非null
// fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullBooleanAsFalse);
// 数值字段如果为null,输出为0,而非null
// fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullNumberAsZero);
// List字段如果为null,输出为[],而非null;
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty);
// 字符类型字段如果为null,输出为"",而非null
// fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty);
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat);
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
/// -> String消息转换器
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setDefaultCharset(CharsetUtil.CHARSET_UTF_8);
stringConverter.setSupportedMediaTypes(fastMediaTypes);
// 将convert添加到converters当中.
converters.add(0, fastJsonHttpMessageConverter);
converters.add(stringConverter);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/metrics");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/null/api-docs",
"/api-docs").setKeepQueryParams(true);
registry.addRedirectViewController("/null/swagger-resources/configuration/ui",
"/swagger-resources/configuration/ui");
registry.addRedirectViewController("/null/swagger-resources/configuration/security",
"/swagger-resources/configuration/security");
registry.addRedirectViewController("/null/swagger-resources", "/swagger-resources");
}
}
片
application.yml 需要配置 数据
spring:
mvc:
throw-exception-if-no-handler-found: true
hiddenmethod:
filter:
enabled: true
resources:
add-mappings: false
servlet:
multipart:
enabled: true
max-file-size: 50MB #最大上传文件大小
max-request-size: 1024MB #批量最大请求大小
必须要配置这个:
spring:
mvc:
hiddenmethod:
filter:
enabled: true