简体   繁体   English

当 objectmapper 读取值时,JsonDeserialize 也设置继承的属性

[英]JsonDeserialize set inherited properties also when objectmapper readvalue

JsonDeserialize not working when objectmapper readvalue for inherited properties.当对象映射器读取继承属性的值时,JsonDeserialize 不起作用。

Vehicle Class车辆等级

@Getter
@Setter
@JsonDeserialize(builder = Vehicle.VehicleBuilder.class)
@Builder(builderClassName = "VehicleBuilder", toBuilder = true)
public class Vehicle{
    private String name;
    private String noOfTyres;
    @JsonPOJOBuilder(withPrefix = "")
    public static class VehicleBuilder{
    }
}

Car class汽车类

@Getter
@Setter
@JsonDeserialize(builder = Car.CarBuilder.class)
@Builder(builderClassName = "CarBuilder", toBuilder = true)
public class Car extends Vehicle {
    private String carType;

    @JsonPOJOBuilder(withPrefix = "")
    public static class CarBuilder extends VehicleBuilder {
    }
}

I don't want to create @NoArgsConstructor ,@AllArgsConstructor in both classes.我不想在两个类中都创建@NoArgsConstructor ,@AllArgsConstructor My issue when Car car = om.readValue(jsonValue,Car.class);我的问题Car car = om.readValue(jsonValue,Car.class); When I parse Json to java object the parent class properties are not setting properly.当我将 Json 解析为 java 对象时,父类属性设置不正确。

As of now I'm using @NoArgsConstructor ,@AllArgsConstructor for work around for the use case.截至目前,我正在使用@NoArgsConstructor ,@AllArgsConstructor来解决用例。

Is there any way to use it along with @JsonDeserialize and @JsonPOJOBuilder ?有什么方法可以将它与@JsonDeserialize and @JsonPOJOBuilder一起使用吗?

The problem with the code is that it assumes that builders in inherited classes will set the parent properties as well.代码的问题在于它假定继承类中的构建器也将设置父属性。 Unfortunately, they don't do that out of the box.不幸的是,他们并没有开箱即用。 However, this is something that can be achieved with Lombok but requires some additional code, as described in this post .然而,这是后话,可与龙目岛可以实现,但需要一些额外的代码,如在此描述的职位

A complete solution could look as follows.一个完整的解决方案可能如下所示。

Parent Class家长班

@Getter
@Setter
@JsonDeserialize
@Builder(builderClassName = "VehicleBuilder", builderMethodName = "vehicleBuilder")
public class Vehicle {
    private String name;
    private String noOfTyres;

}

Child Class儿童班

@Getter
@Setter
@JsonDeserialize(builder = Car.CarBuilder.class)
public class Car extends Vehicle {

    private String carType;

    @Builder
    public Car(String name, String noOfTyres, String carType) {
        super(name, noOfTyres);
        this.carType = carType;
    }

    @JsonPOJOBuilder(withPrefix = "")
    public static class CarBuilder extends VehicleBuilder {
    }

}

Notice that the builder on the extending class is achieved by supplying a constructor with the @Builder annotation.请注意,扩展类上的构建器是通过提供带有 @Builder 注释的构造函数来实现的。 Also take notice that the extending class does not set annotation parameter toBuilder=true as that will require access to parent properties which are private.还要注意,扩展类没有设置注释参数toBuilder=true ,因为这将需要访问私有的父属性。 This can be achieved by setting parent class properties to protected.这可以通过将父类属性设置为 protected 来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM