๐ฃ ๋ฌธ์ ์ํฉ
MapStruct๋ก ๊ฐ์ฒด ๊ฐ์ Mapper ์ฝ๋๋ฅผ ์๋์ผ๋ก ๊ตฌํํ๋ ค๊ณ ํ๋๋ฐ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
MemberMapperImpl.java:46: error: constructor MemberResponseDto in class MemberResponseDto cannot be applied to given types;
MemberResponseDto memberResponseDto = new MemberResponseDto();
^
required: long,String,String,String
found: no arguments
reason: actual and formal argument lists differ in length
๐ ์ฝ๋
// build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mapstruct:mapstruct:1.5.2.Final'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
// MemberMapper.java
package com.codestates.member.mapstruct.mapper;
import com.codestates.member.dto.MemberPatchDto;
import com.codestates.member.dto.MemberPostDto;
import com.codestates.member.dto.MemberResponseDto;
import com.codestates.member.entity.Member;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface MemberMapper {
Member memberPostDtoToMember(MemberPostDto memberPostDto);
Member memberPatchDtoToMember(MemberPatchDto memberPatchDto);
MemberResponseDto memberToMemberResponseDto(Member member);
}
// Member.java
package com.codestates.member.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class Member {
private long memberId;
private String email;
private String name;
private String phone;
}
// MemberPostDto.java
package com.codestates.member.dto;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
public class MemberPostDto {
@NotBlank
@Email
private String email;
@NotBlank(message = "์ด๋ฆ์ ๊ณต๋ฐฑ์ด ์๋์ด์ผ ํฉ๋๋ค.")
private String name;
@Pattern(regexp = "^010-\\d{3,4}-\\d{4}$",
message = "ํด๋ํฐ ๋ฒํธ๋ 010์ผ๋ก ์์ํ๋ 11์๋ฆฌ ์ซ์์ '-'๋ก ๊ตฌ์ฑ๋์ด์ผ ํฉ๋๋ค.")
private String phone;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
// MemberPatchDto.java
package com.codestates.member.dto;
import com.codestates.validator.NotSpace;
import javax.validation.constraints.Pattern;
public class MemberPatchDto {
private long memberId;
@NotSpace(message = "ํ์ ์ด๋ฆ์ ๊ณต๋ฐฑ์ด ์๋์ด์ผ ํฉ๋๋ค")
private String name;
@NotSpace(message = "ํด๋ํฐ ๋ฒํธ๋ ๊ณต๋ฐฑ์ด ์๋์ด์ผ ํฉ๋๋ค")
@Pattern(regexp = "^010-\\d{3,4}-\\d{4}$",
message = "ํด๋ํฐ ๋ฒํธ๋ 010์ผ๋ก ์์ํ๋ 11์๋ฆฌ ์ซ์์ '-'๋ก ๊ตฌ์ฑ๋์ด์ผ ํฉ๋๋ค")
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public long getMemberId() {
return memberId;
}
public void setMemberId(long memberId) {
this.memberId = memberId;
}
}
// MemberResponseDto.java
package com.codestates.member.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class MemberResponseDto {
private long memberId;
private String email;
private String name;
private String phone;
}
๐ง ํด๊ฒฐ ๊ณผ์
๋๋ต 30๋ถ ๊ฐ ์๋ชป ์ ์ ๊ณณ์ด ์๋์ง ๊ณ์ ์ฐพ์๋ณด๊ณ ์ ๋ํ ์ด์ ๋ ์ผ๋ค ์ง์ ๋ค ํ๋ค.
๊ทธ๋ฆฌ๊ณ "mapstruct reason actual and formal argument lists differ in length"๋ฅผ ๊ตฌ๊ธ์ ๊ฒ์ํด ์คํ์ค๋ฒํ๋ก์ฐ์ ๋ธ๋ก๊ทธ๋ค๋ ์ ๋ถ ์ดํด๋ณด์์ง๋ง ํด๊ฒฐํ์ง ๋ชปํ๋ค.
๐ ๊ฒฐ๋ก
๋ต์ build.gradle์ ์์๋ค.
// build.gradle
dependencies {
...
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final'
...
}
์ด์ด์๊ฒ๋ ์์๋ฅผ ๋ฐ๊ฟ๋ณด๋ ์ ์ ๋์ํ๋ค...
MapStruct๋ฅผ ์ฐ๋ ค๋ฉด Getter์ AllArgsConstructor๊ฐ ํ์ํ๋ฐ ์ด๋ฅผ lombok์ด ๋ง๋ค์ด์ฃผ๊ธฐ ์ ์ MapStruct๊ฐ ์ฝ๋๋ฅผ ๊ตฌํํด ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ด๋ค.
build.gradle์ ์์ฑํ annotationProcessor์ ์์๊ฐ Processor์ ์คํ ์์์๋ ๊ด๋ จ์ด ์๋ค๋ ๊ฑด ์ฒ์ ์๊ฒ ๋์๋ค.
"spring annotationProcessor ์์"๋ฅผ ๊ตฌ๊ธ์ ๊ฒ์ํด์ Tistory / [Spring] MapStruct ์ Lombok ์ด ๊ธ์ ์ฐพ์ ์ ์์๋ค. ์ผ์ฐ ๋ฐ๊ฒฌํ์ผ๋ฉด ์ข์์๊ฑธ.. ๊ทธ๋๋ ์๊ฐ์ ๋ง์ด ์ด ๋งํผ, ์์ง ๋ชปํ ์ง์์ ํ๋ ์ป์์ผ๋ ๋ง์กฑํ๋ค.
+ ์ถ๊ฐ
lombok 1.18.16 ์ด์ ๋ฒ์ ๋ถํฐ๋ lombok-mapstruct-binding์ ์ถ๊ฐํด์ฃผ๋ฉด ์์ ์๊ด์์ด ์ธ ์ ์๋ค.
annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0"
Velog / Spring Mapstruct์ lombok ๊ฐ์ด ์ฌ์ฉ์ ๋ฐ์ํ๋ ๋ฌธ์