Created
November 26, 2019 10:08
-
-
Save mikybars/4b9dd00f7ec4c2f304b63b18a8d5bb04 to your computer and use it in GitHub Desktop.
Mapstruct for PATCH operations
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
| <dependency> | |
| <groupId>org.mapstruct</groupId> | |
| <artifactId>mapstruct</artifactId> | |
| <version>${mapstruct.version}</version> | |
| </dependency> |
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
| import io.swagger.v3.oas.annotations.Operation; | |
| import io.swagger.v3.oas.annotations.media.Content; | |
| import io.swagger.v3.oas.annotations.media.Schema; | |
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | |
| import io.swagger.v3.oas.annotations.tags.Tag; | |
| import lombok.RequiredArgsConstructor; | |
| import org.springframework.http.HttpStatus; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.http.converter.HttpMessageNotReadableException; | |
| import org.springframework.web.bind.annotation.*; | |
| import javax.validation.ConstraintViolationException; | |
| import java.util.List; | |
| import static java.util.stream.Collectors.joining; | |
| import static java.util.stream.Collectors.toList; | |
| @RequiredArgsConstructor | |
| @RestController | |
| @RequestMapping(TaskController.BASE_URL) | |
| @Tag(name = "task") | |
| public class TaskController { | |
| public static final String BASE_URL = "/api/task"; | |
| private final TaskService taskService; | |
| private final TaskMapper taskMapper; | |
| // ... | |
| @Operation(summary = "Partial update of a task") | |
| @ApiResponses({ | |
| @ApiResponse(responseCode = "200", description = "Successful operation"), | |
| @ApiResponse(responseCode = "404", description = "Task not found", | |
| content = @Content(schema = @Schema(implementation = ApiError.class))) | |
| }) | |
| @PatchMapping("/{taskId}") | |
| public TaskDto patchTask(@PathVariable Long taskId, @RequestBody TaskDto taskDto) { | |
| Task task = taskMapper.taskDtoToTask(taskDto); | |
| Task savedTask = taskService.update(taskId, task); | |
| return taskMapper.taskToTaskDto(savedTask); | |
| } | |
| // ... | |
| } |
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
| import org.mapstruct.*; | |
| @Mapper | |
| public interface TaskMapper { | |
| @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) | |
| void mergeTasks(Task newTask, @MappingTarget Task oldTask); | |
| } |
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
| import javax.annotation.processing.Generated; | |
| import org.springframework.stereotype.Component; | |
| @Generated( | |
| value = "org.mapstruct.ap.MappingProcessor", | |
| date = "2019-11-26T00:31:50+0100", | |
| comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.4 (Azul Systems, Inc.)" | |
| ) | |
| @Component | |
| public class TaskMapperImpl implements TaskMapper { | |
| @Override | |
| public void mergeTasks(Task newTask, Task oldTask) { | |
| if ( newTask != null ) { | |
| if ( newTask.getId() != null ) { | |
| oldTask.setId( newTask.getId() ); | |
| } | |
| // ... | |
| } | |
| } | |
| } |
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
| import lombok.RequiredArgsConstructor; | |
| import org.springframework.stereotype.Service; | |
| import java.util.List; | |
| @RequiredArgsConstructor | |
| @Service | |
| public class TaskServiceImpl implements TaskService { | |
| private final TaskRepository taskRepository; | |
| private final TaskMapper taskMapper; | |
| // ... | |
| @Override | |
| public Task update(Long id, Task newTask) { | |
| Task savedTask = get(id); | |
| newTask.setId(null); | |
| taskMapper.mergeTasks(newTask, savedTask); | |
| return taskRepository.save(savedTask); | |
| } | |
| // ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment