与杰克逊一起使用不同配置的父母/子类应对父母/子类

发布于 2025-01-28 15:59:41 字数 478 浏览 0 评论 0原文

我正在使用带有Spring Boot的Rentful应用程序进行工作,并且我已经定义了我的类,如下所示:

class Organization {
    String name;
}

class Base {
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    Organization org;
}

class Car extends Base{
  //other members
}

class FlattenCar extends Car {
    @JsonUnwrapped(prefix = "org_")
    Organization org;
}

现在发生的是,对ORG对象的审理取决于第一次调用。这意味着,如果我第一次进行挑选的汽车,那么接下来要打电话给flattencar,反之亦然。 我知道我隐藏了组织成员,但似乎第一次被审理被缓存! 谁知道问题在哪里,我该如何解决?

I'm working on a Restful application with Spring Boot and I've defined my classes as below:

class Organization {
    String name;
}

class Base {
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    Organization org;
}

class Car extends Base{
  //other members
}

class FlattenCar extends Car {
    @JsonUnwrapped(prefix = "org_")
    Organization org;
}

Now what happened is, deserializing the org object depends on the first time call. It means, if I deserialize Car at the first time, next calls on deserializing FlattenCar doesn't unwrap org and vice versa.
I know that I hid org member, but it seems the first time deserializing is cached!
Who knows where is the problem and how can I resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2025-02-04 15:59:41

您的问题似乎是在您的基类中具有read_only(仅用于序列化)的选项,如果只想将此属性考虑到避难所化时。

read_only:访问设置,这意味着只能读取属性
用于序列化,但在避难化过程中未写(集合)。

write_only:访问设置,这意味着该属性只能是
编写(集合)以进行挑选,但不会在
序列化,即该财产的价值不包括在
序列化。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@RestController
public class API {
    
    @Data
    public static class Organization {
        private String name;
    }

    @Data
    public static class Base {
        @JsonProperty(access = Access.WRITE_ONLY)
        private Organization org;
    }
    
    @Getter @Setter
    public static class Car extends Base {
        private String prop1;
    };
    
    @Getter @Setter
    public static class FlattenCar extends Car { 
        private String prop2;
    };
    
    @PostMapping("/car")
    public Car deserializeCar(@RequestBody Car car) {
        System.out.println("Car: " + car);
        return car;
    }
    
    @PostMapping("/flatten_car")
    public FlattenCar deserializeFlattenCar(@RequestBody FlattenCar flattenCar) {
        System.out.println("Flatten: " + flattenCar);
        return flattenCar;
    }
    
}

OUPUT:

Flatten: API.Base(org=API.Organization(name=Org. Test))
Car: API.Base(org=API.Organization(name=Org. Test))

请记住,在此示例中,汽车/Carflatten类的TO_STRING方法已继承了基类。

Your problem seems to be that have in your Base class the option READ_ONLY (only for serialization), when it should be WRITE_ONLY if only want to take this property into account in the deserialization.

READ_ONLY: Access setting that means that the property may only be read
for serialization, but not written (set) during deserialization.

WRITE_ONLY: Access setting that means that the property may only be
written (set) for deserialization, but will not be read (get) on
serialization, that is, the value of the property is not included in
serialization.

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@RestController
public class API {
    
    @Data
    public static class Organization {
        private String name;
    }

    @Data
    public static class Base {
        @JsonProperty(access = Access.WRITE_ONLY)
        private Organization org;
    }
    
    @Getter @Setter
    public static class Car extends Base {
        private String prop1;
    };
    
    @Getter @Setter
    public static class FlattenCar extends Car { 
        private String prop2;
    };
    
    @PostMapping("/car")
    public Car deserializeCar(@RequestBody Car car) {
        System.out.println("Car: " + car);
        return car;
    }
    
    @PostMapping("/flatten_car")
    public FlattenCar deserializeFlattenCar(@RequestBody FlattenCar flattenCar) {
        System.out.println("Flatten: " + flattenCar);
        return flattenCar;
    }
    
}

Ouput:

Flatten: API.Base(org=API.Organization(name=Org. Test))
Car: API.Base(org=API.Organization(name=Org. Test))

Remember that Car/CarFlatten class's to_string method has been inherit of Base class in this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文