This is list of movies.

    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"));
           }

this is update method:

public Result update(Http.Request request, int id) {
        Movie movie = findById(id);
        if (movie == null) {
            return notFound("Movie not Found");
        }
        JsonNode jsonNode = request.body().asJson();
        Movie movie1 = Json.fromJson(jsonNode, Movie.class);
        movie1.setId(id);
        int index = movies.indexOf(movie);
        movies.set(index, movie1);
        return ok(Json.toJson(movie1));
    }

When I send the data using postman,example I will give only movieName then movie will update, but remaining fields will get null value.

But I want to update data, If I did not send the any field it will store with existing value from object.

How can I do this..what is the condition for that

2

There are 2 best solutions below

0
Ashish Bakwad On

Actually, your request in contains the only movieName. And you're not updating that name with movie object. You have to update all values of movie1 to movie object then it will get an update like this.

public Result update(Http.Request request, int id) {
        Movie movie = findById(id);
        if (movie == null) {
            return notFound("Movie not Found");
        }
        JsonNode jsonNode = request.body().asJson();
        Movie movie1 = Json.fromJson(jsonNode, Movie.class);
        movie.setMovieName(movie1.getMovieName()); 
        int index = movies.indexOf(movie);
        movies.set(index, movie);
        return ok(Json.toJson(movie));
    }
2
Luis Iñesta On

Just as a reminder: in RESTful design, there are two similar methods: PUT, that updates your entity with exactly the values you're passing (nulls included, like your current implementation), and PATCH, that only updates the non-value fields.

[better explained: https://medium.com/backticks-tildes/restful-api-design-put-vs-patch-4a061aa3ed0b ]

So my approach would be implement the two variants:

public Result put(Http.Request request, int id) {
   return update(request,id,true);
}
public Result patch(Http.Request request, int id) {
   return update(request,id,false);
}
private Result update(Http.Request request, int id, boolean forceUpdate) {
        Movie existing = findById(id);
        if (existing == null) {
            return notFound("Movie not Found");
        }
        JsonNode jsonNode = request.body().asJson();
        Movie received = Json.fromJson(jsonNode, Movie.class);

        if (forceUpdate || received.getMovieName() != null) {
           existing.setMovieName(received.getMovieName());
        }
        // same for the rest of fields in Movie. You may want to use reflection
        // instead of writing the same for each field manually
        ...

        return ok(Json.toJson(existing));
    }