我正在尝试在同一文档存储(MongoDB)中存储多个相似的文档,因为所有扩展Foo的类都存储在Foo集合中。
我有一个名为findById的休息端点,该端点返回了Optional等类型。 / foo / {id}。
当我调试它时,它将返回Bar类(扩展了Foo
但是,当我从Java客户端应用程序调用它时。
Foo Class
@Data
@Document(collection = "foo")
public abstract class Foo {
@Id
private int id;
public Foo(){}
public Foo(int id){
this.id = id;
}
酒吧班
@Document(collection = "foo")
@Data
public class Bar extends Foo{
private String text;
@PersistenceConstructor
public EnterValue(int id, String text){
super(id);
this.text = text;
}
@Override
public int getId() {
return super.getId();
}
@Override
public int setId(UUID id) {
super.setId(id);
}
public String getText(){
return this.text;
}
public void setText(String text) {
this.text = text;
}
客户代码:
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Foo> response = restTemplate.exchange("http://localhost:8080/foo/123",HttpMethod.GET,entity,Foo.class);
提取类型[class Foo]和内容类型[application / json; charset = UTF-8]的响应时出现错误。
答案 0 :(得分:0)
public class Bar extends Foo implements Serializable {
...
}
...
ResponseEntity<Bar> response = restTemplate.exchange("http://localhost:8080/foo/123", HttpMethod.GET, entity, Bar.class);