【发布时间】:2019-07-10 14:36:24
【问题描述】:
在我的控制器中,我有以下方法:
@RequestMapping(value = "/getAll", method = RequestMethod.GET)
public List<Topic> getAllTopics() {
List<Topic> allTopics = service.getAllTopics();
assert allTopics.size() > 0; // is not empty
System.out.println(allTopics.get(0)); // Topic{id=1, name='bla', description='blahhh'}
return allTopics;
}
当我转到http://localhost:8080/getAll 时,我得到[{},{},{},{}],但service.getAllTopics() 返回非空列表所以要发送的列表不为空,但浏览器接收到无效的JSON。但是,序列化对象没有问题,因为以下方法返回有效的 JSON。有什么问题?
@GetMapping("/json")
public List<Locale> getLocales() {
return Arrays.asList(DateFormat.getAvailableLocales());
}
我正在运行最新的 Spring Boot,即 2.1.3.RELEASE。
更新 这是我的实体类 - 主题
@Entity
@Table(name = "topic", schema="tetra")
public class Topic {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String description;
public Topic() {
}
public Topic(String name, String description) {
this.name = name;
this.description = description;
}
@Override
public String toString() {
return "Topic{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
【问题讨论】:
-
Topic看起来怎么样? -
@KenChan 请查看我的更新答案。
标签: json spring-boot spring-data-jpa