如何从 Json 中判断对象在 SpringBoot 中属于哪个类?

huangapple go评论97阅读模式
英文:

How to know from Json which class an object is in SpringBoot?

问题

我在请求的主体中有以下 JSON 对象

**JSON**

    {
        "nombre": "example",
        "number": 100,
        "listOfMeasurables": {
              "measurableOne": {
                  "positionOne": [0,3,0],
                  "positionTwo": [0,3,0]
              },
              "measurableTwo": {
                  "positionOne": [0,3]
              }
        }
    }

现在我有一个抽象类 Measurable以及 MeasurableOneMeasurableTwo 和 MeasurableThree 类它们都继承自 Measurable

**Measurable**

    public abstract class Measurable {
        public abstract String getType();
    }

**MeasurableOne**

    public class MeasurableOne extends Measurable {
        protected int [] positionOne;
        protected int [] positionTwo;

        public MeasurableOne(int [] positionOne, int [] positionTwo) {
            this.positionOne = positionOne;
            this.positionTwo = positionTwo;
        }

        @Override
        public String getType() {
            return "MeasurableOne";
        }
    }

**MeasurableTwo**

    public class MeasurableTwo extends Measurable {
        protected int [] positionOne;

        public MeasurableTwo(int [] positionOne) {
            this.positionOne = positionOne;
        }

        @Override
        public String getType() {
            return "MeasurableTwo";
        }
    }

**MeasurableThree**

    public class MeasurableThree extends Measurable {
        protected int [] positionOne;
        protected int [] positionTwo;
        protected int [] positionThree;

        public MeasurableThree(int [] positionOne, int [] positionTwo, int [] positionThree) {
            this.positionOne = positionOne;
            this.positionTwo = positionTwo;
            this.positionThree = positionThree;
        }

        @Override
        public String getType() {
            return "MeasurableThree";
        }
    }

现在我有一个控制器将接收此 JSONlistOfMeasurables 数组可以包含 measurableOnemeasurableTwomeasurableThree 中的任意一个也可以是其中的 12 或 3 个顺序任意如何让控制器知道可测量对象的类型以便创建相应的对象

    @PostMapping(path = "/createActivity")
    public ResponseEntity<String> createActivity(@RequestBody Activity activity) { // 在这里
    }

非常感谢您的帮助谢谢
英文:

I have the following JSON object in the body of a request:

JSON

{
&quot;nombre&quot;: &quot;example&quot;,
&quot;number&quot;: 100,
&quot;listOfMeasurables&quot;:{
&quot;measurableOne&quot;:{
&quot;positionOne&quot;:[0,3,0],
&quot;positionTwo&quot;:[0,3,0]
},
&quot;measurableTwo&quot;:{
&quot;positionOne&quot;:[0,3]
}
}
}

Now i have Class Measurable which is Abstract, and MeasurableOne, MeasurableTwo, and MeasurableThree, which extends Measurable.

Measurable

public abstract class Measurable {
public abstract String getType();
}

MeasurableOne

public class MeasurableOne extends Measurable {
protected int [] positionOne;
protected int [] positionTwo;
public MeasurableOne(int [] positionOne, int [] positionTwo) {
this.positionOne = positionOne;
this.positionTwo = positionTwo;
}
@Override
public String getType() {
return &quot;MeasurableOne&quot;;
}
}

MeasurableTwo

public class MeasurableTwo extends Measurable {
protected int [] positionOne;
public MeasurableTwo(int [] positionOne) {
this.positionOne = positionOne;
}
@Override
public String getType() {
return &quot;MeasurableTwo&quot;;
}
}

MeasurableThree

public class MeasurableThree extends Measurable {
protected int [] positionOne;
protected int [] positionTwo;
protected int [] positionThree;
public MeasurableThree(int [] positionOne, int [] positionTwo, int [] positionThree) {
this.positionOne = positionOne;
this.positionTwo = positionTwo;
this.positionThree = positionThree;
}
@Override
public String getType() {
return &quot;MeasurableThree&quot;;
}
}

Now, i have a controller, which will receive this json. The listOfMeasurables array, can contain either measurableOne, measurableTwo, measurableThree, 1, 2 or 3 of them, in any order. How can i make the controller know which type the measurables be in order to create that object?

@PostMapping(path = &quot;/createActivity&quot;)
public ResponseEntity&lt;String&gt; createActivity(@RequestBody Activity activity) { -&gt;&gt; HERE
}

Any help is appreciated! Thanks

答案1

得分: 2

你的Activity可以通过以下代码进行定义,jackson会处理它:

public class Activity {
    private String nombre;
    private Integer number;
    private ListOfMeasurables listOfMeasurables;

    // getter and setter
}

public class ListOfMeasurables {
    private MeasurableOne measurableOne;
    private MeasurableTwo measurableTwo;
    private MeasurableThree measurableThree;

    // getter and setter
}
英文:

your Activity can be defined with following code, the jackson will handle it

public class activity{
private String nombre;
private Integer number;
private ListOfMeasurables listOfMeasurables;
// getter and setter
}
public class ListOfMeasurables {
private MeasurableOne measurableOne;
private MeasurableTwo measurableTwo;
private MeasurableThree measurableThree;
// getter and setter 
}

huangapple
  • 本文由 发表于 2020年10月16日 06:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64380414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定