jackson配置

This commit is contained in:
谭凯凯
2024-11-21 09:54:02 +08:00
committed by Tankaikai
parent 870c1d604e
commit 449e36ab4a

View File

@@ -1,9 +1,15 @@
package cn.ysk.cashier.config.interceptor; package cn.ysk.cashier.config.interceptor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.TimeZone;
/** /**
* @author lyf * @author lyf
*/ */
@@ -13,4 +19,25 @@ public class WebConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry){ public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new UserInterceptor()); registry.addInterceptor(new UserInterceptor());
} }
@Bean
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
//忽略未知属性
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//日期格式转换
//mapper.setDateFormat(new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN));
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//Long类型转String类型
//SimpleModule simpleModule = new SimpleModule();
//simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//mapper.registerModule(simpleModule);
converter.setObjectMapper(mapper);
return converter;
}
} }