【发布时间】:2019-06-20 19:51:30
【问题描述】:
我正在尝试将多个相似的文档存储在同一个文档存储 (MongoDB) 中,即所有扩展 Foo 的类都存储在 Foo 集合中。
我有一个名为 findById 的休息端点,它返回一种可选类型,例如/foo/{id}。
当我调试它时,它返回一个 Bar 类(它扩展了 Foo
但是,当我从 java 客户端应用程序调用它时。
Foo 类
@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] 的响应时出现错误。
【问题讨论】:
-
请同时发布错误和控制器。
标签: java spring mongodb rest inheritance