实体类
public class ClueResp {
@JSONField(format="yyyy-MM-dd")
private Date happendDate;
private Date operateDate;
……省略 set get
}
配置类
@Configuration
public class FastJsonConfiguration {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty);
fastJsonConfig.setSerializeFilters(new ValueFilter() {
// Map<class,Map<filed,dateFormat>>
Map<Class, Map<String, String>> dateFormatCache = new ConcurrentHashMap<>();
@Override
public Object process(Object obj, String key, Object value) {
if (value instanceof Date) {
Class<?> objClass = obj.getClass();
if (dateFormatCache.get(objClass) == null) {
dateFormatCache.putIfAbsent(objClass, new HashMap<>());
Map<String, String> newFormatMap = dateFormatCache.get(objClass);
ReflectionUtils.doWithFields(obj.getClass(), (field) -> {
if (field.getType() == Date.class) {
JSONField annotation = field.getAnnotation(JSONField.class);
if (annotation != null) {
String format = annotation.format();
if (StringUtils.isNotBlank(format)) {
newFormatMap.putIfAbsent(field.getName(), format);
}
}
}
});
}
Map<String, String> dateFormatMap = dateFormatCache.get(objClass);
if (!dateFormatMap.isEmpty()) {
String format = dateFormatMap.get(key);
if(StringUtils.isNotBlank(format)) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(value);
}
}
}
return value;
}
});
//中文乱码解决方案
List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
//设定Json格式且编码为utf-8
fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMedisTypes);
//将转换规则应用于转换对象
fastConverter.setFastJsonConfig(fastJsonConfig);
//long返回给前端转换为String
SerializeConfig serializeConfig = fastJsonConfig.getSerializeConfig();
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastConverter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(fastConverter);
}
}
主要代码
fastJsonConfig.setSerializeFilters(new ValueFilter() {
// Map<class,Map<filed,dateFormat>>
Map<Class, Map<String, String>> dateFormatCache = new ConcurrentHashMap<>();
@Override
public Object process(Object obj, String key, Object value) {
if (value instanceof Date) {
Class<?> objClass = obj.getClass();
if (dateFormatCache.get(objClass) == null) {
dateFormatCache.putIfAbsent(objClass, new HashMap<>());
Map<String, String> newFormatMap = dateFormatCache.get(objClass);
ReflectionUtils.doWithFields(obj.getClass(), (field) -> {
if (field.getType() == Date.class) {
JSONField annotation = field.getAnnotation(JSONField.class);
if (annotation != null) {
String format = annotation.format();
if (StringUtils.isNotBlank(format)) {
newFormatMap.putIfAbsent(field.getName(), format);
}
}
}
});
}
Map<String, String> dateFormatMap = dateFormatCache.get(objClass);
if (!dateFormatMap.isEmpty()) {
String format = dateFormatMap.get(key);
if(StringUtils.isNotBlank(format)) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(value);
}
}
}
return value;
}
});
结果
{
"code": 0,
"data": {
"happendDate": "2020-11-29",
"operateDate": "2020-11-29 11:14:33"
},
"message": "成功"
}