13693261870
2 天以前 80247f1b5e00fab2ce6c9e8474cfeceefb902d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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;
    }
}