1

As I read fron documentation (https://spring.io/guides/gs/accessing-data-rest/)

PUT replaces an entire record. Fields not supplied will be replaced with null. PATCH can be used to update a subset of items.

So, I try to use PATCH for my User entity with 2 fields (name and surname)

@RequestMapping(path="/user/{id}", method = RequestMethod.PATCH)
public User updateUser ( User user) {       
    return userRepository.save(user);
}

When I send to:

localhost:8080/user/34

body with name: "user" and surname: "testSurname" it works well. but when I send only name (without surname), surname also updates to null.

How can I prevent spring boot from update fields with null values?

Sever
  • 2,338
  • 5
  • 35
  • 55
  • `userRepository.save(department)` what is `department` ? did you mean `userRepository.save(user)` ? – Manos Nikolaidis Jun 16 '17 at 10:25
  • 2
    The guide you have linked to is about Spring Data REST which provides RESTful endpoints for you. In your question you have written your own endpoint where it's up to you to decide how `PATCH` should behave – Andy Wilkinson Jun 16 '17 at 10:30
  • I've put together a [post](https://cassiomolin.com/using-http-patch-in-spring/) that describes an approach for using `PATCH` in Spring. And a working example is available on [GitHub](https://github.com/cassiomolin/http-patch-spring). – cassiomolin Jan 09 '20 at 13:24

1 Answers1

2

You're not using Spring Data Rest here. You're using spring data jpa repository userRepository.save(department);

and Spring MVC by creating your custom http endpoint.

@RequestMapping(path="/user/{id}", method = RequestMethod.PATCH)
public User updateUser ( User user) {...

To see the patch behavior that you described you have to use the endpoint that is exposed by spring data rest, not a custom one that you've made. When you create a custom endpoint it will do exactly what you code it to do, spring will not mess with your code and alter the behavior.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • I cant find any useful info about that. Can you give some example please ? – Sever Jun 16 '17 at 11:11
  • @Sever You can find a lot of info on the official pages of the projects - http://projects.spring.io/spring-data-rest/ and http://projects.spring.io/spring-data-jpa/ You may want to check the concepts of the projects (what they're suppose to do). The guides that you've already found are also a good resource. – Evgeni Dimitrov Jun 16 '17 at 11:24
  • Hi, Is it possible to have a custom http endpoint but still have spring update the entity with the new values from the HTTP request? So i can manually process the now updated entity in the customer method? – nissim_dev Feb 23 '18 at 05:22
  • @user2049132 Sorry I didn't understand the question – Evgeni Dimitrov Feb 23 '18 at 08:55
  • @EvgeniDimitrov I want to use a custom http endpoint but have spring merge the updated values from the request to the entity when i use it in the method. Inside this method `@RequestMapping(path="/user/{id}", method = RequestMethod.PATCH) public User updateUser ( User user) {` – nissim_dev Feb 23 '18 at 10:29
  • @EvgeniDimitrov I want to do something similar to the answer [in this question](https://stackoverflow.com/questions/31768289/spring-data-jpa-how-to-update-a-model-elegantly) but for REST API without using modelattribute – nissim_dev Mar 07 '18 at 13:21
  • @user2049132 The equivalent of `@ModelAttribute` in rest services is `@RequestBody`. Try that – Evgeni Dimitrov Mar 07 '18 at 14:00