๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Today I Learned

mapper

 

Today I Learn โœ๐Ÿผ 

 

  • ์˜ค๋Š˜ ํ•˜๋ฃจ ๊ฐ€์žฅ ์ธ์ƒ ๊นŠ์—ˆ๋˜ ๋ฐฐ์›€์—๋Š” ๋ญ๊ฐ€ ์žˆ์—ˆ์ง€?

 

  • ๊ทธ ๋ฐฐ์›€๊นŒ์ง€ ๋‹ค๊ฐ€๊ฐ€๋Š”๋ฐ ์–ด๋–ค ์–ด๋ ค์›€์ด ์žˆ์—ˆ์ง€?

 

  • ๊ทธ ์–ด๋ ค์›€์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋‚˜์˜ ์‹œ๋„๋“ค์€ ๋ฌด์—‡์ด ์žˆ์—ˆ์ง€?

 

  • ๊ทธ ๊ณผ์ •์—์„œ ๋‚˜๋Š” ๋ฌด์—‡์„ ๊นจ๋‹ฌ์•˜๊ณ , ์–ด๋–ค ๊ฐ์ •/์ƒ๊ฐ์ด ๋“ค์—ˆ์—ˆ์ง€?

 

  • ์ด ์ƒํƒœ์—์„œ ์ดํ›„ ๋” ๋‚˜์€ ๋‚ด๊ฐ€ ๋˜๋ ค๋ฉด ๋ฌด์—‡์„ ๋ณด์™„ํ•˜์ง€?

 


 

package com.coopang.user.presentation.request;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Getter
@Setter
public class SignupRequestDto {
    @NotBlank(message = "Email is required")
    @Email(message = "Email should be valid")
    private String email;

    @NotBlank(message = "Password is required")
    @Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,15}$",
            message = "Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character")
    private String password;

    @NotBlank(message = "Username is required")
    @Pattern(regexp = "^[a-zA-Z๊ฐ€-ํžฃ ]{2,10}$", message = "Username must contain only letters, Korean characters, and optional spaces")
    private String userName;
    private String phoneNumber;

    @NotBlank(message = "Role is required.")
    private String role;

    private String slackId;

    private String zipCode;
    private String address1;
    private String address2;

    private UUID nearHubId;
}

 

 

 

package com.coopang.user.application.request;

import lombok.Builder;
import lombok.Getter;

import java.util.UUID;

@Getter
public class UserDto {
    private UUID userId;
    private String email;
    private String password;
    private String userName;
    private String phoneNumber;
    private String role;
    private String slackId;

    private String zipCode;
    private String address1;
    private String address2;
    private UUID nearHubId;

    @Builder
    public UserDto(
        UUID userId
        , String email
        , String password
        , String userName
        , String phoneNumber
        , String role
        , String slackId
        , String zipCode
        , String address1
        , String address2
        , UUID nearHubId
    ) {
        this.userId = userId;
        this.email = email;
        this.password = password;
        this.userName = userName;
        this.phoneNumber = phoneNumber;
        this.role = role;
        this.slackId = slackId;
        this.zipCode = zipCode;
        this.address1 = address1;
        this.address2 = address2;
        this.nearHubId = nearHubId;
    }

    public static UserDto of(
        UUID userId
        , String email
        , String password
        , String userName
        , String phoneNumber
        , String role
        , String slackId
        , String zipCode
        , String address1
        , String address2
        , UUID nearHubId
    ) {
        return UserDto.builder()
            .userId(userId)
            .email(email)
            .password(password)
            .userName(userName)
            .phoneNumber(phoneNumber)
            .role(role)
            .slackId(slackId)
            .zipCode(zipCode)
            .address1(address1)
            .address2(address2)
            .nearHubId(nearHubId)
            .build();
    }

    @Override
    public String toString() {
        return "UserDto{" +
            "userId=" + userId +
            ", email='" + email + '\'' +
            ", password='" + password + '\'' +
            ", userName='" + userName + '\'' +
            ", phoneNumber='" + phoneNumber + '\'' +
            ", role='" + role + '\'' +
            ", slackId='" + slackId + '\'' +
            ", zipCode='" + zipCode + '\'' +
            ", address1='" + address1 + '\'' +
            ", address2='" + address2 + '\'' +
            ", nearHubId=" + nearHubId +
            '}';
    }

    public void createId(UUID userId) {
        this.userId = userId;
    }
}

 

 

 

org.modelmapper.MappingException: ModelMapper mapping errors:

1) Failed to instantiate instance of destination com.coopang.user.application.request.UserDto. Ensure that com.coopang.user.application.request.UserDto has a non-private no-argument constructor.

1 error

 


org.modelmapper.MappingException์ด ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ModelMapper๊ฐ€ ๋งคํ•‘ํ•  ๋•Œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. UserDto ํด๋ž˜์Šค์— ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์—†์–ด์„œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.

ModelMapper๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ no-argument constructor(๊ธฐ๋ณธ ์ƒ์„ฑ์ž)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ํ•„๋“œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ UserDto ํด๋ž˜์Šค๋Š” @Builder๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ , ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ModelMapper๊ฐ€ ์ด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•:

  1. ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ์ถ”๊ฐ€: UserDto์— ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ModelMapper๊ฐ€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”.