龙目岛+杰克逊一成不变 [英] Lombok + Jackson immutables

查看:106
本文介绍了龙目岛+杰克逊一成不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将项目更新为Spring Boot 1.5.10之后 龙目岛停止了与杰克逊的正常合作. 我的意思是当我的对象中的字段名称与json请求中的字段不同时,创建不可变的DTO:

After updating my project to Spring Boot 1.5.10 Lombok stopped working correctly with Jackson. I mean immutable DTOs creation, when field names in my objects are not same as fields in json request:

@Value
@Builder
public class MyImmutableDto implements Serializable {

    @JsonProperty("other-field-1-name")
    private final BigDecimal myField1;

    @JsonProperty("other-field-2-name")
    private final String myField2;

    and a lot of fields there...
}

因此,在将Spring Boot更新到1.5.10之后,此代码无法正常工作,我需要像这样配置lombok:

So, after updating Spring Boot to 1.5.10 this code isn't working, and I need to configure lombok like that:

lombok.anyConstructor.addConstructorProperties = true

有人知道在没有此lombok修复的情况下,使用jackson + lombok创建此类对象的任何其他方法吗? 除了此修复程序,我可以使用以下代码:@JsonPOJOBuilder@JsonDeserialize(builder = MyDto.MyDtoBuilder.class):

Does anyone know any other way to create such objects with jackson + lombok without this lombok fix? Instead of this fix I can use following code: @JsonPOJOBuilder and @JsonDeserialize(builder = MyDto.MyDtoBuilder.class):

@Value
@Builder
@JsonDeserialize(builder = MyDto.MyDtoBuilder.class)
public class MyDto implements Serializable {

    // @JsonProperty("other-field-1-name")    // not working
    private final BigDecimal myField1;

    private final String myField2;
    private final String myField3;
    and a lot of fields there...

    @JsonPOJOBuilder(withPrefix = "")
    public static final class MyDtoBuilder {
    }
}

但是它不适用于@JsonProperty("other-field-1-name"). Ofc,可以通过简单的@JsonCreator完成,但是也许可以通过一些构造函数/jackson注释将其与lombok一起使用?

But it is not working with @JsonProperty("other-field-1-name"). Ofc, it can be done by simple @JsonCreator, but maybe there is some way to use it with lombok using some constructor/jackson annotations?

推荐答案

所以这不是完全相同的情况,但这可以解决我的问题.我需要在构建器上使用@JsonDeserialize批注,将其放在构建器上可以显式解决此问题(以样板代码为代价).至少我不需要输入其余的构建器.

So this is not the exact same case, but this works for my problem. I need the @JsonDeserialize annotation on the builder, putting it there on the builder explicitly solves the problem (at the cost of boilerplate code). At least I don't need to type out the rest of the builder.

@Value
@Builder
@JsonDeserialize(builder = ProductPrice.ProductPriceBuilder.class)
public class ProductPrice {

    @JsonSerialize(using = MoneySerializer.class)
    @JsonDeserialize(using = MoneyDeserializer.class)
    Money price;

    Duration rentalLength;

    Period recurrence;

    @JsonPOJOBuilder(withPrefix = "")
    public static class ProductPriceBuilder{
        @JsonDeserialize(using = MoneyDeserializer.class)
        public ProductPrice.ProductPriceBuilder price(Money price) {
            this.price = price;
            return this;
        }  
    }
}

这篇关于龙目岛+杰克逊一成不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆