springboot 版本 2.7.3
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
dependency>
因为springBoot做了jar包版本管理,所以不需要引入version标签
spring.mvc.converters.preferred-json-mapper: gson
@Configuration
public class GsonConfig {
/**
* 自定义gson配置
*/
@Bean
public GsonBuilderCustomizer customizer() {
return b -> b
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
// The date format will be used to serialize and deserialize Date and in case the java.sql module is present, also java.sql
// .Timestamp and java.sql.Date.
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.serializeNulls()
.create();
}
/**
* 只有序列化功能
*/
// public static class LocalDateTimeSerializer implements JsonSerializer {
//
// @Override
// public JsonElement serialize(LocalDateTime localDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
// return new JsonPrimitive(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
// }
// }
//
// public static class LocalDateSerializer implements JsonSerializer {
//
// @Override
// public JsonElement serialize(LocalDate localDate, Type type, JsonSerializationContext jsonSerializationContext) {
// return new JsonPrimitive(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
// }
// }
public static class LocalDateAdapter extends TypeAdapter<LocalDate> {
@Override
public void write(final JsonWriter jsonWriter, final LocalDate localDate) throws IOException {
if (localDate == null) {
jsonWriter.nullValue();
} else {
jsonWriter.value(localDate.toString());
}
}
@Override
public LocalDate read(final JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
} else {
return LocalDate.parse(jsonReader.nextString());
}
}
}
public static class LocalDateTimeAdapter extends TypeAdapter<LocalDateTime> {
@Override
public void write(final JsonWriter jsonWriter, final LocalDateTime localDate) throws IOException {
if (localDate == null) {
jsonWriter.nullValue();
} else {
jsonWriter.value(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
@Override
public LocalDateTime read(final JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
} else {
return LocalDateTime.parse(jsonReader.nextString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
}
}
@Data
public class Book {
private Long id;
private String name;
//@JsonAdapter(value = GsonConfig.LocalDateAdapter.class)
private LocalDate publishDate;
//@JsonAdapter(value = GsonConfig.LocalDateTimeAdapter.class)
private LocalDateTime createTime;
}
@RestController
@RequestMapping("/book")
public class IndexController {
@PostMapping
public Book save(@RequestBody Book book) {
return book;
}
@GetMapping
public Book get() {
Book book = new Book();
book.setId(1L);
book.setName("tcoding");
book.setCreateTime(LocalDateTime.now());
book.setPublishDate(LocalDate.now());
return book;
}
}
curl 127.0.0.1:8080/book
{"id":"1","name":"tcoding","publishDate":"2022-09-10","createTime":"2022-09-10 15:32:17"}%
curl -X POST 127.0.0.1:8080/book --header 'Content-Type: application/json' --data-raw '{"id":"1","name":"tcoding","publishDate":"2022-09-10","createTime":"2022-09-10 15:32:17"}'
{"id":"1","name":"tcoding","publishDate":"2022-09-10","createTime":"2022-09-10 15:32:17"}%

上面GsonConfig配置类里面
@Bean
public GsonBuilderCustomizer customizer() {
return b -> b
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
// The date format will be used to serialize and deserialize Date and in case the java.sql module is present, also java.sql
// .Timestamp and java.sql.Date.
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.serializeNulls()
.create();
}
是为了注入到GsonBuilder
根据 GsonBuilder生成Gson


yaml配置 spring.mvc.converters.preferred-json-mapper的值为gson
则自动生成 GsonHttpMessageConverter bean


源码地址 https://github.com/googalAmbition/hello-spring-boot/tree/main/31-gson