[英]How to deserialize Json with a nested array of objects in a simple Spring Boot application
The Json POST request looks like this: 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'
}
]
}
So my Spring Boot Application just wants to store this whole json as a "Film" class and inside it has an inline array of "Actors".因此,我的 Spring Boot 应用程序只想将整个 json 存储为“Film”类,并在其中包含一个“Actors”内联数组。 Here is the Film model:
这是电影模型:
@Entity
public class Film {
@Id
@GeneratedValue
private long id;
private String title;
private String description;
private ArrayList<Actor> actors = new ArrayList<>();
I have a separate entity for the Actor that looks similar:我有一个单独的 Actor 实体,看起来很相似:
@Entity
public class Actor {
@Id
@GeneratedValue
private long id;
private String name;
private String lastName;
Finally, I am using the RequestBody Annotation in the PostMapping in the Controller:最后,我在控制器的 PostMapping 中使用了 RequestBody Annotation:
@PostMapping(value= "/api/film")
@ResponseStatus(HttpStatus.CREATED)
public Film addFilm(@RequestBody Film film) {
service.createFilm(film);
return film;
The problem is I always get the java.io.NotSerializableException that Actor cannot be serialized.问题是我总是得到无法序列化 Actor 的 java.io.NotSerializableException 。 I tried making Actor a Static inline class but that did not change anything.
我尝试使 Actor 成为静态内联类,但这并没有改变任何东西。 Anyone have an idea what is wrong here ?
有人知道这里有什么问题吗?
Your Actor class needs to implement Serializable.您的 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.