【问题标题】:Inheritance in Json Response Spring bootJson Response Spring boot 中的继承
【发布时间】:2020-06-08 04:54:05
【问题描述】:

我有两个班级,A 和 B:

class A{
    private int numberOne;
    private int numberTwo;
    public int getNumberOne() {
        return numberOne;
    }
    public void setNumberOne(int numberOne) {
        this.numberOne = numberOne;
    }
    public int getNumberTwo() {
        return numberTwo;
    }
    public void setNumberTwo(int numberTwo) {
        this.numberTwo = numberTwo;
    }
    }

    class B extends A {

    private int numberThree;
    public int getNumberThree() {
        return numberThree;
    }
    public void setNumberThree(int numberThree) {
        this.numberThree = numberThree;
    }
}

我怎么能喜欢这样:

    ResponseEntity<A> someMethod(){
        return new B(1,2,3);
    }
json 
    {
     "numberOne":"1",
     "numberTwo":"2"
    }

    ResponseEntity<B> someMethod(){
        return new B(1,2,3);
    }
json 
    {
     "numberOne":"1",
     "numberTwo":"2",
     "numberThree":"3"
    } 

如何在我想要的 Spring Boot 中使用 JSON 忽略?

【问题讨论】:

    标签: json spring-boot inheritance jackson-databind


    【解决方案1】:

    你创建了新的类

    public class Views {
    public static class Public {
    }
    
    public static class Internal extends Public {
     }
    }
    

    A.类

    public class A {
    
    public A(int numberOne, int numberTwo) {
        this.numberOne = numberOne;
        this.numberTwo = numberTwo;
    }
    
    @JsonView(Views.Public.class)
    private int numberOne;
    @JsonView(Views.Public.class)
    private int numberTwo;
    
    public int getNumberOne() {
        return numberOne;
    }
    
    public void setNumberOne(int numberOne) {
        this.numberOne = numberOne;
    }
    
    public int getNumberTwo() {
        return numberTwo;
    }
    
    public void setNumberTwo(int numberTwo) {
        this.numberTwo = numberTwo;
    }
    

    }

    B.类

    public class B extends A{
    
    @JsonView(Views.Internal.class)
    private int numberThree;
    
    public B(int numberOne, int numberTwo) {
        super(numberOne, numberTwo);
    }
    
    public B(int numberOne, int numberTwo, int numberThree) {
        super(numberOne, numberTwo);
        this.numberThree = numberThree;
    }
    
    public int getNumberThree() {
        return numberThree;
    }
    
    public void setNumberThree(int numberThree) {
        this.numberThree = numberThree;
    }
    

    }

    控制器

     @GetMapping("/a-method")
    @JsonView(Views.Public.class)
    public ResponseEntity<A> getA(){
        return ResponseEntity.ok(new B(1,2,3));
    }
    
    @GetMapping("/b-method")
    @JsonView(Views.Internal.class)
    public ResponseEntity<B> getB(){
        return ResponseEntity.ok(new B(1,2,3));
    }
    

    【讨论】:

    • 谢谢兄弟,按照你的例子我用这个:@JsonView(A.class)@JsonView(B.class)
    猜你喜欢
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2017-08-28
    • 2017-06-11
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多