package com.terra.common.config;
|
|
import com.alibaba.fastjson2.JSONReader;
|
import com.alibaba.fastjson2.JSONWriter;
|
import com.alibaba.fastjson2.support.config.FastJsonConfig;
|
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
|
|
import com.terra.common.interceptor.AuthInterceptor;
|
import com.terra.common.service.CommonService;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.core.Ordered;
|
import org.springframework.http.MediaType;
|
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.util.AntPathMatcher;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.springframework.web.servlet.config.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.io.IOException;
|
import java.nio.charset.StandardCharsets;
|
import java.util.Collections;
|
import java.util.List;
|
|
/**
|
* WebConfig:通过实现该接口并重写方法来扩展Spring MVC配置,不会覆盖默认配置。
|
*/
|
@Configuration
|
public class WebConfig implements WebMvcConfigurer {
|
@Resource
|
CommonService commonService;
|
|
@Override
|
public void addViewControllers(ViewControllerRegistry registry) {
|
// 设置访问路径为 “/” 跳转到指定页面
|
registry.addViewController("/").setViewName("redirect:/sign/toIndex");
|
// 设置为最高优先级
|
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
}
|
|
/**
|
* 获取拦截器对象
|
*/
|
public AuthInterceptor getAuthBean() {
|
return new AuthInterceptor(commonService);
|
}
|
|
/**
|
* swagger控制
|
*/
|
@Override
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
//registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
registry.addResourceHandler("/druid/**").addResourceLocations("classpath:/META-INF/resources/druid/");
|
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
|
}
|
|
/**
|
* 忽略url地址大小写
|
*/
|
@Override
|
public void configurePathMatch(PathMatchConfigurer configurer) {
|
AntPathMatcher matcher = new AntPathMatcher();
|
matcher.setCaseSensitive(false);
|
|
configurer.setPathMatcher(matcher);
|
}
|
|
@Override
|
public void addInterceptors(InterceptorRegistry registry) {
|
registry.addInterceptor(getAuthBean())
|
//.excludePathPatterns("/v3/api-docs")
|
.addPathPatterns("/**");
|
}
|
|
/**
|
* 处理json格式,值null的转换为""
|
*/
|
@Override
|
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
//FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter() {
|
@Override
|
protected void writeInternal(Object object, org.springframework.http.HttpOutputMessage outputMessage)
|
throws IOException, org.springframework.http.converter.HttpMessageNotWritableException {
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
if (attributes != null) {
|
String requestURI = attributes.getRequest().getRequestURI();
|
if ("/v3/api-docs".equals(requestURI)) { // 排除 /v3/api-docs 路径
|
// 使用默认的 JSON 序列化(如 Jackson),这里需要手动处理,不能直接调用 super.writeInternal
|
outputMessage.getBody().write(String.valueOf(object).getBytes());
|
return;
|
}
|
}
|
super.writeInternal(object, outputMessage); // 其他路径使用 Fastjson2 处理
|
}
|
};
|
converter.setFastJsonConfig(getFastJsonConfig());
|
converter.setDefaultCharset(StandardCharsets.UTF_8);
|
converter.setSupportedMediaTypes(Collections.singletonList(
|
MediaType.APPLICATION_JSON
|
));
|
converters.add(0, converter);
|
}
|
|
private static FastJsonConfig getFastJsonConfig() {
|
FastJsonConfig config = new FastJsonConfig();
|
// 配置序列化特征
|
config.setWriterFeatures(
|
JSONWriter.Feature.WriteNullListAsEmpty,
|
//JSONWriter.Feature.WriteNullBooleanAsFalse,
|
//JSONWriter.Feature.WriteNullNumberAsZero,
|
//JSONWriter.Feature.PrettyFormat, // 格式化输出
|
JSONWriter.Feature.WriteMapNullValue // 输出空字段
|
);
|
// 反序列化特性配置
|
config.setReaderFeatures(
|
JSONReader.Feature.FieldBased,
|
JSONReader.Feature.SupportArrayToBean
|
);
|
return config;
|
}
|
}
|