[英]How to deserialize Json with a nested array of objects in a simple Spring Boot application
Json POST 请求如下所示:
{
'title':'Star Wars: The Empire Strikes Back',
'description':'Darth Vader is adamant about turning Luke Skywalker to the dark side.',
'actors':[
{
'lastName':'Ford',
'name':'Harrison'
},
{
'lastName':'Hamill',
'name':'Mark'
}
]
}
因此,我的 Spring Boot 应用程序只想将整个 json 存储为“Film”类,并在其中包含一个“Actors”内联数组。 这是电影模型:
@Entity
public class Film {
@Id
@GeneratedValue
private long id;
private String title;
private String description;
private ArrayList<Actor> actors = new ArrayList<>();
我有一个单独的 Actor 实体,看起来很相似:
@Entity
public class Actor {
@Id
@GeneratedValue
private long id;
private String name;
private String lastName;
最后,我在控制器的 PostMapping 中使用了 RequestBody Annotation:
@PostMapping(value= "/api/film")
@ResponseStatus(HttpStatus.CREATED)
public Film addFilm(@RequestBody Film film) {
service.createFilm(film);
return film;
问题是我总是得到无法序列化 Actor 的 java.io.NotSerializableException 。 我尝试使 Actor 成为静态内联类,但这并没有改变任何东西。 有人知道这里有什么问题吗?
您的 Actor 类需要实现 Serializable。
@Entity
public class Actor implements Serializable{
@Id
@GeneratedValue
private long id;
private String name;
private String lastName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.