gpt4 book ai didi

java - 当我在 postman 中发送相同的电影名称时如何编写条件我不会接受相同的电影名称

转载 作者:行者123 更新时间:2023-11-30 01:42:02 34 4
gpt4 key购买 nike

这是电影课

。我有使用发布请求在 postman 中添加电影的代码。

public class Movie {


private int id;
private String movieName;
private String description;
private String hero;
private String heroine;

public Movie(){

}

public Movie(int id, String movieName, String description, String hero, String heroine) {
this.id = id;
this.movieName = movieName;
this.description = description;
this.hero = hero;
this.heroine = heroine;

}
//here have setter and getter methods

这是 Controller 类。现在不接受重复的电影名称的逻辑是什么

public class MovieController extends Controller {

public static List<Movie> movies;

static {
movies = new ArrayList<>();
movies.add(new Movie(1, "Fprd vs Ferrari", "Movie on Racing", "abcd", "xyz"));
movies.add(new Movie(2, "F2", "Comedy Movie", "Venkatesh", "Tamanna"));
movies.add(new Movie(3, "Titanic", "Movie", "Hero", "Heroine"));
movies.add(new Movie(4, "Saaho", "action", "Prabhas", "Shradda kapoor"));
movies.add(new Movie(5, "Bhahubali 1", "action", "Prabhas", "Tamanna"));

}
public Result insert(Http.Request request) {
JsonNode jsonNode = request.body().asJson();
if (jsonNode == null) {
return badRequest("insufficient movie information");
}
Movie movie = Json.fromJson(jsonNode, Movie.class);
movies.add(movie);
return ok(Json.toJson(movie));
}

这是post请求的路由

POST        /newmovies              controllers.MovieController.insert(request: Request)

最佳答案

你可以这样做:

Movie movie = Json.fromJson(jsonNode, Movie.class);

if(movies.stream().noneMatch(m -> m.getMovieName().equals(movie.getName()))) {
movies.add(movie);
return ok(Json.toJson(movie));
} else {
// Movie name already exists... Throw exception here
return badRequest("Movie already exists");
}

关于java - 当我在 postman 中发送相同的电影名称时如何编写条件我不会接受相同的电影名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59607768/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com