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๊ฐ ์ด๋ฅผ ์์ฑํ์ง ๋ชปํ๊ณ ์์ต๋๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ:
- ๊ธฐ๋ณธ ์์ฑ์ ์ถ๊ฐ: UserDto์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ถ๊ฐํ๋ฉด ModelMapper๊ฐ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ์๋์ ๊ฐ์ด ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ถ๊ฐํด ์ฃผ์ธ์.
'Today I Learned' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
local ํตํฉ ํ ์คํธ (0) | 2024.10.25 |
---|---|
์ฌ๋์ผ๋ก ๋ฉ์ธ์ง ๋ณด๋ด๊ธฐ (0) | 2024.10.23 |
gateway ๋ผ์ฐํ ์๋ฌ ํด๊ฒฐํ๊ธฐ (0) | 2024.10.16 |
ํ์ ๊ณผ์ ์์์ ๋ฌธ์ ํด๊ฒฐ๊ณผ ์นดํ์นด ๋ฉ์์ง ๊ตฌ์กฐ ๊ฐ์ (0) | 2024.10.14 |
@Component, @Configuration + @ConfigurationProperties (0) | 2024.10.02 |