|
@@ -1,8 +1,14 @@
|
|
|
package space.anyi.config;
|
|
package space.anyi.config;
|
|
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
|
+import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
|
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
@@ -12,6 +18,10 @@ import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
|
|
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
|
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
|
|
import org.thymeleaf.templatemode.TemplateMode;
|
|
import org.thymeleaf.templatemode.TemplateMode;
|
|
|
|
|
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @fileName: SpringMvcConfig
|
|
* @fileName: SpringMvcConfig
|
|
|
* @projectName: SSM_template
|
|
* @projectName: SSM_template
|
|
@@ -67,4 +77,23 @@ public class SpringMvcConfig implements WebMvcConfigurer {
|
|
|
// .addPathPatterns("/**")
|
|
// .addPathPatterns("/**")
|
|
|
// .excludePathPatterns("/login", "/register");
|
|
// .excludePathPatterns("/login", "/register");
|
|
|
}
|
|
}
|
|
|
|
|
+ // 配置消息转换器,支持JSON
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
|
|
+ MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
|
|
|
|
+
|
|
|
|
|
+ // 配置ObjectMapper
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ // 格式化输出
|
|
|
|
|
+ objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
|
|
+ // 日期格式
|
|
|
|
|
+ objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
+ // 忽略空字段
|
|
|
|
|
+ objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
|
|
+
|
|
|
|
|
+ converter.setObjectMapper(objectMapper);
|
|
|
|
|
+ //converter.setSupportedMediaTypes(List.of(MediaType.APPLICATION_JSON));
|
|
|
|
|
+ converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
|
|
|
|
|
+ converters.add(converter);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|