Commit a082b3b1 by tyfanchz

为 jackson 增加读取配置的支持

1 parent e6e5377f
...@@ -3,12 +3,10 @@ package com.glsx.admin.config; ...@@ -3,12 +3,10 @@ package com.glsx.admin.config;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties; import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -21,45 +19,21 @@ import org.springframework.context.annotation.Configuration; ...@@ -21,45 +19,21 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration @Configuration
public class JacksonMapperConfig { public class JacksonMapperConfig {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
@Value("${spring.jackson.time-zone:GMT+8}")
private String timeZone;
@Autowired(required = false) @Autowired(required = false)
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Autowired @Autowired(required = false)
private JacksonProperties jacksonProperties; private JacksonProperties jacksonProperties;
@PostConstruct @PostConstruct
public void init() { public void init() {
if (objectMapper != null) { if (objectMapper != null && jacksonProperties != null) {
//objectMapper.setDateFormat(new SimpleDateFormat(pattern));
//objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));
//objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
// false);
//objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
// false);
this.configDateFormat(objectMapper);
this.configTimeZone(objectMapper);
this.configSerialization(objectMapper, jacksonProperties.getSerialization()); this.configSerialization(objectMapper, jacksonProperties.getSerialization());
this.configDeserialization(objectMapper, jacksonProperties.getDeserialization()); this.configDeserialization(objectMapper, jacksonProperties.getDeserialization());
this.configOthers(objectMapper, jacksonProperties);
} }
} }
private void configDateFormat(ObjectMapper objectMapper) {
objectMapper.setDateFormat(new SimpleDateFormat(pattern));
}
private void configTimeZone(ObjectMapper objectMapper) {
objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));
}
private void configSerialization(ObjectMapper objectMapper, private void configSerialization(ObjectMapper objectMapper,
Map<SerializationFeature, Boolean> features) { Map<SerializationFeature, Boolean> features) {
features.forEach(objectMapper::configure); features.forEach(objectMapper::configure);
...@@ -69,4 +43,10 @@ public class JacksonMapperConfig { ...@@ -69,4 +43,10 @@ public class JacksonMapperConfig {
Map<DeserializationFeature, Boolean> features) { Map<DeserializationFeature, Boolean> features) {
features.forEach(objectMapper::configure); features.forEach(objectMapper::configure);
} }
private void configOthers(ObjectMapper objectMapper, JacksonProperties properties) {
objectMapper.setDateFormat(new SimpleDateFormat(properties.getDateFormat()));
objectMapper.setTimeZone(properties.getTimeZone());
objectMapper.setLocale(properties.getLocale());
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!