我有一个要执行此操作的Maven Java Spring Rest项目。
@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”},// <-这是开发人员
{id:2,env:“ Linux”},// <-这是DevOps
]
而不仅仅是
[
{id:1},// <-这是开发人员
{id:2},// <-这是DevOps
]
答案 0 :(得分:1)
试图重现您的问题,但未成功:
@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
,因此我只能假设您正确创建了它们。
答案 1 :(得分:0)
我找到了解决方案,在我的现实生活中,专有财产是私人的,我忘记了制造吸气剂。