title: update specific data for an object using POST method in spring boot rest
body: <p>I want to update specific data only <code>name</code> field for a restaurant object. The way I want to make it that if a field is <strong>empty or null</strong>, just leave the old value.</p>
  <p>Is there any good solution?</p>
  <p>Note that I'm making a rest api app with Spring Boot.</p>
  <p><strong>Restaurant.java:</strong></p>
  <pre class="lang-java prettyprint-override"><code>@Entity
  @NoArgsConstructor
  @RequiredArgsConstructor
  @Getter
  @Setter
  @ToString
  public class Restaurant {
  
      @Id
      @GeneratedValue
      private long id;
  
      @NonNull
      @NotEmpty(message = &quot;The restaurant must have a name&quot;)
      private String name;
  
      @NonNull
      @NotEmpty(message = &quot;Please add a description for this restaurant&quot;)
      private String description;
  
      @NonNull
      @NotEmpty(message = &quot;The restaurant must have a location&quot;)
      private String location;
  }
  </code></pre>
  <p><strong>My post update function:</strong></p>
  <pre class="lang-java prettyprint-override"><code>@PostMapping(&quot;/restaurant/{id}/update&quot;)
  public Restaurant updateRestaurant(@PathVariable Long id, @RequestBody Restaurant restaurantDetails, BindingResult bindingResult) {     
      Optional&lt;Restaurant&gt; restaurantOptional = restaurantService.findById(id);
  
      if (restaurantOptional.isPresent()) {
          Restaurant restaurant = restaurantOptional.get();
          restaurant.setName(restaurantDetails.getName());
          restaurant.setLocation(restaurant.getLocation());
          restaurant.setDescription(restaurantDetails.getDescription());
          logger.info(&quot;restaurant information edited successfully&quot;);
          return restaurantService.save(restaurant);
      } else
          return null;
  } 
  </code></pre>