英文:
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,以及 MeasurableOne、MeasurableTwo 和 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";
}
}
现在,我有一个控制器,将接收此 JSON。listOfMeasurables 数组可以包含 measurableOne、measurableTwo、measurableThree 中的任意一个,也可以是其中的 1、2 或 3 个,顺序任意。如何让控制器知道可测量对象的类型,以便创建相应的对象?
@PostMapping(path = "/createActivity")
public ResponseEntity<String> createActivity(@RequestBody Activity activity) { // 在这里
}
非常感谢您的帮助!谢谢
英文:
I have the following JSON object in the body of a request:
JSON
{
"nombre": "example",
"number": 100,
"listOfMeasurables":{
"measurableOne":{
"positionOne":[0,3,0],
"positionTwo":[0,3,0]
},
"measurableTwo":{
"positionOne":[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 "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";
}
}
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 = "/createActivity")
public ResponseEntity<String> createActivity(@RequestBody Activity activity) { ->> 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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论