Java Spring Boot - 自定义 JSON 序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39627170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:18:03  来源:igfitidea点击:

Spring Boot - Custom JSON Serialization

javaspringspring-mvcspring-bootHymanson

提问by Punter Vicky

I generally use mixins to perform custom serialization and deserialization when using Hymanson Library. My RestController in Spring Boot app has methods similar to one listed below. I guess Spring Boot uses Hymanson to serialize the VerifyAccountResponse into string. However this converts my calendar / date objects into a long value when they are converted to String. I am able to convert them into appropriate format by using custom serializer. However I am having to change the return type into an object after serialization. Is there a way to retain the same signature and add custom serializer to default serialization performed by Spring Boot.

在使用 Hymanson Library 时,我通常使用 mixins 来执行自定义序列化和反序列化。我在 Spring Boot 应用程序中的 RestController 具有类似于下面列出的方法。我猜 Spring Boot 使用 Hymanson 将 VerifyAccountResponse 序列化为字符串。但是,这会将我的日历/日期对象转换为字符串时转换为长值。我可以使用自定义序列化程序将它们转换为适当的格式。但是,我必须在序列化后将返回类型更改为对象。有没有办法保留相同的签名并将自定义序列化程序添加到 Spring Boot 执行的默认序列化。

@RequestMapping(value ="verifyAccount", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<VerifyAccountResponse> verifyAccount(@RequestBody VerifyAccountRequest request) {

    VerifyAccountResponse response = service.verifyAccount(request);

    return new ResponseEntity<VerifyAccountResponse>(response, HttpStatus.OK);
}

EDIT:

编辑:

Updated the below based on the answers , but mixin doesn't seem to take effect -

@Bean
public Hymanson2ObjectMapperBuilder objectMapperBuilder() {
    Hymanson2ObjectMapperBuilder builder = new Hymanson2ObjectMapperBuilder();

    builder.mixIn(ConnectStatus.class, com.datacast.service.util.DateFormatSerializerMixin.class);

    return builder;
}

EDIT 2:

编辑2:

I created a simple spring boot project to test this out and this works fine. But when I use this approach in my larger project , the date conversion is not happening. Could there be anything overriding Hymanson2ObjectMapperBuilder ?

我创建了一个简单的 spring boot 项目来测试这个,这很好用。但是当我在更大的项目中使用这种方法时,日期转换没有发生。有什么可以覆盖 Hymanson2ObjectMapperBuilder 吗?

采纳答案by Lakatos Gyula

You can customize the Hymanson serializer in a spring boot application in a lot of ways. Please consider checking the documentation regarding Hymanson in the spring boot reference guide:

您可以通过多种方式在 Spring Boot 应用程序中自定义 Hymanson 序列化程序。请考虑查看 spring boot 参考指南中有关 Hymanson 的文档:

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto.html#howto-customize-the-Hymanson-objectmapper

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto.html#howto-customize-the-Hymanson-objectmapper

You can configure a custom serializer by using Hymanson2ObjectMapperBuilder.

您可以使用Hymanson2ObjectMapperBuilder配置自定义序列化程序

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/json/Hymanson2ObjectMapperBuilder.html#serializerByType-java.lang.Class-com.fasterxml.Hymanson.databind.JsonSerializer-

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/json/Hymanson2ObjectMapperBuilder.html#serializerByType-java.lang.Class-com.fasterxml.Hymanson.databind。 JsonSerializer-

回答by dmytro

You can customize date format (as I understand it's the main reason of your post) by setting property

您可以通过设置属性来自定义日期格式(据我所知,这是您发布帖子的主要原因)

spring.Hymanson.date-format= 
# Date format string or a fully-qualified date format class name.
For instance `yyyy-MM-dd HH:mm:ss`.