打补丁时如何正确使用dto?

时间:2021-07-28 11:42:15

标签: java spring spring-boot dto

我有UserDto

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "UserDto", description = " DTO User ")
public class UserDto {

    private Long userId;
    private String firstName;
    private String lastName;
    private LocalDate dateOfBirth;
    private String education;
    private String aboutMe;

我需要创建更新方法。这就是我现在所拥有的。

@PatchMapping("/{user}/edit")
    public ResponseEntity<String> update(@RequestBody UserDto userDto, @PathVariable long id) {
        Optional<User> optionalUser = userService.getById(id);
        if (!optionalUser.isPresent()) {
            return ResponseEntity
                    .badRequest()
                    .body("Пользователь не найден");
        }
        User user = optionalUser.get();
        userService.update(user);
        return new ResponseEntity<>(HttpStatus.OK);
    }

如何使用 Dto 来部分更新用户数据?我想我需要一个转换器。谢谢!

1 个答案:

答案 0 :(得分:0)

您必须在Entity类中创建一个构造函数并将字段从dto转换为entity