|
@@ -1,29 +1,23 @@
|
|
|
package com.yonge.cooleshow.cms.config;
|
|
|
|
|
|
-import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-import com.yonge.cooleshow.cms.config.jackson.JacksonConfig;
|
|
|
-import com.yonge.cooleshow.cms.config.jackson.MappingJSONHttpMessageConverter;
|
|
|
-import com.yonge.cooleshow.cms.interceptor.OperationLogInterceptor;
|
|
|
-import com.yonge.cooleshow.common.config.EnumConverterFactory;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.format.FormatterRegistry;
|
|
|
import org.springframework.http.MediaType;
|
|
|
-import org.springframework.http.converter.HttpMessageConverter;
|
|
|
-import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import com.yonge.cooleshow.cms.interceptor.OperationLogInterceptor;
|
|
|
+import com.yonge.cooleshow.common.config.EnumConverterFactory;
|
|
|
|
|
|
@Configuration
|
|
|
public class WebMvcConfig implements WebMvcConfigurer {
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private OperationLogInterceptor operationLogInterceptor;
|
|
|
|
|
@@ -32,65 +26,22 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
|
|
*/
|
|
|
@Override
|
|
|
public void addFormatters(FormatterRegistry registry) {
|
|
|
- // 枚举转换器
|
|
|
registry.addConverterFactory(new EnumConverterFactory());
|
|
|
- // 字符串时间转换器
|
|
|
- registry.addConverter(new JacksonConfig.CustomStringDateConverter());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
registry.addInterceptor(operationLogInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param converters List<HttpMessageConverter<?>>
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
- //WebMvcConfigurer.super.configureMessageConverters(converters);
|
|
|
-
|
|
|
- // 如果存在fastJson的转换器,将其移除
|
|
|
- converters.removeIf(httpMessageConverter -> httpMessageConverter instanceof FastJsonHttpMessageConverter);
|
|
|
-
|
|
|
- // 需要重新加入jackson的转换器,该处的objectMapper已经在配置中注册了
|
|
|
- MappingJSONHttpMessageConverter converter = new MappingJSONHttpMessageConverter(new JacksonConfig.JacksonObjectMapper());
|
|
|
- converter.setDefaultCharset(StandardCharsets.UTF_8); // 字符编号
|
|
|
- converters.add(0, converter);
|
|
|
-
|
|
|
- // 添加String转换器
|
|
|
- converters.add(utf8StringHttpMessageConverter());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 自定义String转换器
|
|
|
- * @return HttpMessageConverters
|
|
|
- */
|
|
|
- //@Bean
|
|
|
- @Primary
|
|
|
- public StringHttpMessageConverter utf8StringHttpMessageConverter() {
|
|
|
-
|
|
|
- StringHttpMessageConverter converter = new StringHttpMessageConverter();
|
|
|
-
|
|
|
- List<MediaType> mediaTypes = Lists.newArrayList();
|
|
|
- mediaTypes.add(MediaType.TEXT_HTML);
|
|
|
- mediaTypes.add(MediaType.TEXT_PLAIN);
|
|
|
- mediaTypes.add(MediaType.TEXT_XML);
|
|
|
- mediaTypes.add(MediaType.APPLICATION_XML);
|
|
|
-
|
|
|
- converter.setSupportedMediaTypes(mediaTypes);
|
|
|
- converter.setDefaultCharset(StandardCharsets.UTF_8);
|
|
|
-
|
|
|
- return converter;
|
|
|
- }
|
|
|
-
|
|
|
- //@Bean
|
|
|
- public HttpMessageConverters fastJsonHttpMessageConverters(){
|
|
|
+ @Bean
|
|
|
+ public HttpMessageConverters fastJsonHttpMessageConverters(){
|
|
|
LocalFastJsonHttpMessageConverter converter = new LocalFastJsonHttpMessageConverter();
|
|
|
- List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
|
|
|
- fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
|
|
|
- converter.setSupportedMediaTypes(fastMediaTypes);
|
|
|
- return new HttpMessageConverters(converter);
|
|
|
- }
|
|
|
+ List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
|
|
|
+ fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
|
|
|
+ converter.setSupportedMediaTypes(fastMediaTypes);
|
|
|
+ return new HttpMessageConverters(converter);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
+
|