我有一个Maven项目,我想在这里这样做。
@RestController
@RequestMapping("/person")
public class PersonController {
@GetMapping
public List<Person> getPerson(){
List<Person> persons = new ArrayList<>();
persons.add(new Dev(1, "Java")); //Let's say I have the constructor
persons.add(new DevOps(2, "Linux")); //Let's say I have the constructor
return implants;
}
}
public class Person{ public int id;}
public class Dev extends Person{ public string lang;}
public class DevOps extends Person{ public string env;}
我希望结果是
[
{id : 1,lang:"Java"},//<-这是Dev
{id : 2,env:"Linux"},//<-这是DevOps
]
而不仅仅是
[
{id : 1},//<-这是Dev
{id : 2},//<-这是DevOps
]
发布于 2018-11-22 13:37:43
试图复制你的问题,但没有成功:
@RestController
@RequestMapping("/person")
public class PersonController {
@GetMapping
public List<Person> getPerson(){
List<Person> persons = new ArrayList<>();
persons.add(new Dev(1, "Java")); //Let's say I have the constructor
persons.add(new DevOps(2, "Linux")); //Let's
return persons;
}
}
public class Person {
public int id;
public Person(int id) {
this.id = id;
}
}
public class Dev extends Person {
public String lang;
public Dev(int id, String lang) {
super(id);
this.lang = lang;
}
}
public class DevOps extends Person {
public String env;
public DevOps(int id, String env) {
super(id);
this.env = env;
}
}
结果:
http://localhost:8080/person/
[{"id":1,"lang":"Java"},{"id":2,"env":"Linux"}]
你确定你把所有相关代码都贴出来了吗?既然您声明了Let's say I have the constructor
,我只能假设您正确地创建了它们。
发布于 2018-11-22 13:49:27
我找到了解决办法,在我的现实生活中,礼仪是私密的,而我忘了创造更多的东西。
https://stackoverflow.com/questions/53431891
复制相似问题