目前java json解析工具有阿里的fastjson,google的GSON,以及SpringMVC 默认的解析工具Jackson。
spring-boot-starter-web默认引入了以下依赖:
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.8.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.8.7version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.8.7version>
dependency>
Jackson从2.0开始改用新的包名fasterxml;1.x版本的包名是codehaus。
在spring boot项目里建议全局初始化ObjectMapper
@Configuration
public class AppConfig{
@Bean
public ObjectMapper ObjectMapper(){
ObjectMapper objectMapper=new ObjectMapper();
return objectMapper;
}
}
//将json字符串转换成对象
Map map = objectMapper.readValue(jsonString, Map.class);
//转换对象类型
SomethingPOJO pojo = objectMapper.convertValue(map, SomethingPOJO.class);
//将对象转换成json字符串
Sting string = objectMapper.writeValueAsString(pojo);
//将json字符串转换成List
JavaType javaType = mapper.getTypeFactory()
.constructParametricType(List.class, Person.class);
List<Person> jsonToPersonList = objectMapper.readValue(arrayToJson, mapType);
public enum TypeEnumWithValue {
TYPE1(1, “Type A”), TYPE2(2, “Type 2”);
private Integer id;
private String name;
/**
* @JsonValue只能标识在类的唯一个方法上,表示序列化时使用当前方法转换本对象。
*/
@JsonValue
public String getName() {
return name;
}
/**
* @JsonCreator标识在一个静态方法上,表示反序列化时使用当前方法初始化本对象。
*/
@JsonCreator
public static TypeEnumWithValue create(String val) {
TypeEnumWithValue [] units = TypeEnumWithValue.values();
for (TypeEnumWithValue unit : units) {
if (unit.getName().equals(val)) {
return unit;
}
}
throw new IllegalArgumentException();
}
}
ObjectMapper objectMapper=new ObjectMapper();
// 忽略json字符串中不识别的属性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 忽略无法转换的对象 “No serializer found for class com.xxx.xxx”
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
在JDK8项目中使用JDK8新特性的话,jackson需要新增依赖包。
<dependency>
<groupId>com.fasterxml.jackson.modulegroupId>
<artifactId>jackson-module-parameter-namesartifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatypegroupId>
<artifactId>jackson-datatype-jdk8artifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatypegroupId>
<artifactId>jackson-datatype-jsr310artifactId>
<version>2.8.6version>
dependency>
@Configuration
public class AppConfig{
@Bean
public ObjectMapper ObjectMapper(){
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
return objectMapper;
}
}
也可以使用自动注册的方式:
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
jdk8使用了新的时间包,在时间对象上需要做的配置。
import java.time.LocalDateTime;
import com.fasterxml.jackson.annotation.JsonFormat
public class POJO{
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime time;
//getter,setter...
}
在springboo1.1以上的版本默认配置了Jackson的ObjectMapper,不必自行定义bean。
可以这么做全局设置:
spring:
jackson:
serialization:
WRITE_DATES_AS_TIMESTAMPS: false
default-property-inclusion: non_null
更多配置参见 org.springframework.boot.autoconfigure.jackson.JacksonProperties
参考文档
- jackson JDK8: https://github.com/FasterXML/jackson-modules-java8
- Jackson JSON Tutorial: http://www.baeldung.com/jackson
- Springboot统一配置Jackson:https://www.cnblogs.com/woshimrf/p/springboot-jackson-global-config.html
- Spring Boot中Jackson应用详解: https://blog.51cto.com/7308310/2310930