如何通过Postman修复Spring Boot中的更新过程(一对多,一对一)



我在更新电影时遇到问题。

我写了一个函数,它以";"更新";在MovieService。

这是下面显示的函数。

public void update(Long id,Movie movie) {
boolean isUpdatingEmployee = (movie.getId() == id);
if (isUpdatingEmployee) {
Movie existingMovie = movieRepository.findById(movie.getId()).get();
existingMovie.setId(id);
existingMovie.setName(movie.getName());
existingMovie.setRating(movie.getRating());
existingMovie.setDirector(movie.getDirector());
existingMovie.setGenres(movie.getGenres());
existingMovie.setCreatedAt(movie.getCreatedAt());
movieRepository.save(existingMovie);
}
}

当ı在保存后尝试更新电影时,我得到了这种JSON结果,这就是为什么无法完成更新过程。

http://localhost:8082/api/v1/movie/update/1

正文请求

{
"name": "MovieC",
"genres": [
{ 
"name" : "Adventure"
},
{
"name" : "Action"
}
],
"createdAt": "2021-04-28",
"rating" : 9,
"director" : {
"name" : "Director 2"
}     
}

更新进程后的JSON结果。

{
"id": null,
"name": "MovieC",
"genres": [
{
"id": null,
"name": "Action"
},
{
"id": null,
"name": "Adventure"
}
],
"rating": 9.0,
"createdAt": "2021-04-28",
"director": {
"id": null,
"name": "Director 2"
}
}

这是我的电影实体,如下所示。

@Entity
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Movie implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@JsonManagedReference
@OneToMany(mappedBy="movie",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Genre> genres;
private Double rating;
private LocalDate createdAt;

@ManyToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn
private Director director;
}

这是我的董事实体,如下所示。

@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties({"movies"})
public class Director implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String name;
@OneToMany(mappedBy="director",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Movie> movies;
}

这是我的流派实体,如下所示。

@Entity
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties({"movie"})
public class Genre implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String name;
@JsonBackReference
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn
private Movie movie;
}

这是我的示例项目链接:项目链接

我该怎么修?

根据您的代码,这是您的请求:

http://localhost:8082/api/v1/movie/update/1

{
"name": "MovieC",
"genres": [
{ 
"name" : "Adventure"
},
{
"name" : "Action"
}
],
"createdAt": "2021-04-28",
"rating" : 9,
"director" : {
"name" : "Director 2"
}     
}

现在考虑一下代码中的这个片段:

public void update(Long id,Movie movie) {
boolean isUpdatingEmployee = (movie.getId() == id);
if (isUpdatingEmployee) {
...

您的id将是1,因为您已经在路径变量中设置了它。但是,movie.getId()将为空,因为我在您的RequestBody中没有看到它。

因此:

isUpdatingEmployee = (movie.getId() == id)` 
isUpdatingEmployee = (    null      ==  1)
isUpdatingEmployee = false

这总是会给你false,所以我认为这不会进入你的更新逻辑。

我认为问题是因为您返回的对象电影与您在控制器中post方法的主体中传递的对象电影相同-https://github.com/Rapter1990/springboothazelcast/blob/3157f354a628d418cccb99cfdbd188f594c24e9c/src/main/java/com/springboot/hazelcast/controller/MovieController.java#L64

你应该把它重写成这样:

@PostMapping("/save")
public Movie saveMovie(@RequestBody Movie movie) throws ParseException {
LOG.info("MovieController | Saving Movie.");
return movieService.save(movie);
}

以下是CRUDRepository javadocs的链接:https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html#save-S-

最新更新