JacksonMapperConfig.java
2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.glsx.admin.config;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Configuration;
/**
* <p>Description: 用于配置 jackson 序列化和反序列化
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-05-14
*/
@Configuration
public class JacksonMapperConfig {
@Autowired(required = false)
private ObjectMapper objectMapper;
@Autowired(required = false)
private JacksonProperties jacksonProperties;
@PostConstruct
public void init() {
if (objectMapper != null && jacksonProperties != null) {
this.configSerialization(objectMapper, jacksonProperties.getSerialization());
this.configDeserialization(objectMapper, jacksonProperties.getDeserialization());
this.configOthers(objectMapper, jacksonProperties);
}
}
private void configSerialization(ObjectMapper objectMapper,
Map<SerializationFeature, Boolean> features) {
features.forEach(objectMapper::configure);
}
private void configDeserialization(ObjectMapper objectMapper,
Map<DeserializationFeature, Boolean> features) {
features.forEach(objectMapper::configure);
}
private void configOthers(ObjectMapper objectMapper, JacksonProperties properties) {
if (properties.getDateFormat() != null) {
objectMapper.setDateFormat(new SimpleDateFormat(properties.getDateFormat()));
}
if (properties.getTimeZone() != null) {
objectMapper.setTimeZone(properties.getTimeZone());
}
if (properties.getLocale() != null) {
objectMapper.setLocale(properties.getLocale());
}
}
}