This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/api/people") | |
public class PeopleController { | |
... | |
@PatchMapping("{id}") | |
public ResponseEntity<Void> updatePerson(@PathVariable("id") long id, | |
@Valid @RequestBody PersonUpdateDto personUpdateDto) { | |
Optional<Person> personOptional = peopleRepository.getById(id); | |
if (!personOptional.isPresent()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.partialupdates.utils; | |
import org.openapitools.jackson.nullable.JsonNullable; | |
import java.util.function.Consumer; | |
public final class JsonNullableUtils { | |
private JsonNullableUtils() {} | |
public static <T> void changeIfPresent(JsonNullable<T> nullable, Consumer<T> consumer) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/api/people") | |
public class PeopleController { | |
... | |
@PatchMapping("{id}") | |
public ResponseEntity<Void> updatePerson(@PathVariable("id") long id, | |
@Valid @RequestBody PersonUpdateDto personUpdateDto) { | |
Optional<Person> personOptional = peopleRepository.getById(id); | |
if (!personOptional.isPresent()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.partialupdates.controllers.dtos; | |
import org.openapitools.jackson.nullable.JsonNullable; | |
import javax.validation.constraints.NotNull; | |
import java.time.OffsetDateTime; | |
@SuppressWarnings("FieldMayBeFinal") | |
public class PersonUpdateDto { | |
@NotNull |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.partialupdates.config; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.openapitools.jackson.nullable.JsonNullableModule; | |
import org.springframework.context.annotation.Configuration; | |
import javax.annotation.PostConstruct; | |
@Configuration | |
public class JacksonConfiguration { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person person = personOptional.get(); | |
if (personUpdateDto.getFirstName() != null) { | |
person.setFirstName(personUpdateDto.getFirstName()); | |
} | |
if (personUpdateDto.getLastName() != null) { | |
person.setLastName(personUpdateDto.getLastName()); | |
} | |
if (personUpdateDto.getBirthday() != null) { | |
person.setBirthday(personUpdateDto.getBirthday()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 1, | |
"firstName": "Henrick", | |
"lastName": "Kakutalua", | |
"birthday": "2020-06-02T22:10:16.016+01:00", | |
"bio": "Lorem ipsum", | |
"imageUrl": "https://image/profile_photo.jpg" | |
}, | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.partialupdates.controllers; | |
import com.example.partialupdates.controllers.dtos.PersonReadDto; | |
import com.example.partialupdates.controllers.dtos.PersonUpdateDto; | |
import com.example.partialupdates.domain.Person; | |
import com.example.partialupdates.repositories.PeopleRepository; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.*; | |
import javax.validation.Valid; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.partialupdates.controllers.dtos; | |
import java.time.OffsetDateTime; | |
public class PersonUpdateDto { | |
private String firstName; | |
private String lastName; | |
private OffsetDateTime birthday; | |
private String bio; | |
private String imageUrl; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.partialupdates.controllers.dtos; | |
import java.time.OffsetDateTime; | |
public class PersonReadDto { | |
private final long id; | |
private final String firstName; | |
private final String lastName; | |
private final OffsetDateTime birthday; | |
private final String bio; |