[BackEnd][Maven-PJ] ์‚ฌ๋‚ด ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ํšŒ์› ๊ด€๋ จ : ํšŒ์› ๊ฐ€์ž…

2022. 3. 31. 08:00ใ†Programming Project ์ž‘์—…์‹ค/๋‚ด์šฉ ์ •๋ฆฌ

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ป Git Hub ์ฃผ์†Œ

 

 

๐Ÿ—‚ ๋ชฉ์ฐจ

โ— [BackEnd][Maven-PJ] ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ : ๊ธ€ ๋“ฑ๋ก(์ž‘์„ฑ) / ์ˆ˜์ •

โ— [BackEnd][Maven-PJ] ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ : ๋ชฉ๋ก ์กฐํšŒ

โ— [BackEnd][Maven-PJ]๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ : ์ƒ์„ธ ์กฐํšŒ

โ— [BackEnd][Maven-PJ]๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ : ์‚ญ์ œ

โ— [BackEnd][Maven-PJ] ์‚ฌ๋‚ด ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ : ๋‹ต๋ณ€ ๋“ฑ๋ก / ์ˆ˜์ •

โ— [BackEnd][Maven-PJ] ์‚ฌ๋‚ด ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ : ๋‹ต๋ณ€ ์‚ญ์ œ

 

๐Ÿ‘จ‍๐Ÿ‘ฉ‍๐Ÿ‘ง‍๐Ÿ‘ฆ ํšŒ์› ๊ด€๋ จ

โ— [BackEnd][Maven-PJ] ์‚ฌ๋‚ด ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ํšŒ์› ๊ด€๋ จ : ํšŒ์› ๊ฐ€์ž…

 

 

๐Ÿ“š๋ถ€ ๋ก 

โ— [BackEnd][Maven-PJ]๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ ์„œ๋น„์Šค - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ : Paging์ฒ˜๋ฆฌ

โ— [Spring] @Valid๋ฅผ ์ด์šฉํ•œ @RequestBody ๊ฒ€์ฆ / Exception Handling / ์ •๊ทœ ํ‘œํ˜„์‹


๐Ÿค” ๋‚ด๊ฐ€ ๋งŒ๋‚œ Exception

โ— [Exception ์ •๋ฆฌ] org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

โ— [Vue.js][Spring] Post Data ์ „์†ก ๊ฐ„ null๊ฐ’ ์ „์†ก ๋ฌธ์ œ

 

 

 

 

 

 

๐Ÿš€ ํšŒ์› ๊ด€๋ จ - ํšŒ์› ๊ฐ€์ž…

    ๐Ÿ”ฝ  Data Base Table ๊ตฌ์„ฑ

user Table

 

 

 

 

    ๐Ÿ”ฝ  Swagger

 

 

 

 

 

    ๐Ÿ”ฝ  Code ๋ถ„์„

        ๐Ÿ“ฆ CustomUserDetails.java

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {        // ์ด์šฉ์ž ๊ถŒํ•œ ๋ชฉ๋ก ๋ฐ˜ํ™˜ Method

        ArrayList<GrantedAuthority> auth = new ArrayList<>();

        auth.add(new SimpleGrantedAuthority(authority));

        return auth;
    } // getAuthorities() ๋

    @Override
    public boolean isAccountNonExpired() {     		// ๊ณ„์ • ๋งŒ๋ฃŒ ์—ฌ๋ถ€ | true : ๋งŒ๋ฃŒ ์•ˆ๋จ, false : ๋งŒ๋ฃŒ
        return true;
    } // isAccountNonExpired() ๋

    @Override
    public boolean isAccountNonLocked() {    		 // ๊ณ„์ • ์ž ๊น€ ์—ฌ๋ถ€ | true : ์•ˆ ์ž ๊น€, false : ์ž ๊น€
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {     // ๋น„๋ฐ€๋ฒˆํ˜ธ ๋งŒ๋ฃŒ ์—ฌ๋ถ€ | true : ๋งŒ๋ฃŒ ์•ˆ๋จ, false : ๋งŒ๋ฃŒ
        return true;
    } // isCredentialsNonExpired() ๋

    @Override
    public boolean isEnabled() {         			// ๊ณ„์ • ํ™œ์„ฑํ™” ์—ฌ๋ถ€ | true : ํ™œ์„ฑํ™”, false : ๋น„ ํ™œ์„ฑํ™”
        return true;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }

    public String getUserKind() {
        return userKind;
    }

    public void setUserKind(String userKind) {
        this.userKind = userKind;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

} // class ๋

 

์ฃผ๋‹ˆํ•˜๋ž‘์€ ํšŒ์›๊ฐ€์ž… ๋ฐ Login ์‹œ ์ด์šฉ๋  Value Object ๊ฐ์ฒด๋ฅผ `UserDetails`๋ฅผ ๊ตฌํ˜„ํ•œ Class๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด์—์š”.

 

 

 

        ๐Ÿ“ฆ DuplicateByIdInfoDTO.java

 

์ด DTO๋Š” ํšŒ์›๊ฐ€์ž… ์‹œ์— ์ค‘๋ณต ํ™•์ธ๋“ฑ์— ํ™•์ธ ์ ˆ์ฐจ์—์„œ ์ด์šฉ๋  ๊ฐ’๋“ค์„ Meber ๋ณ€์ˆ˜๋กœ ์ด์šฉํ•˜๋Š” DTO์ธ ๊ฒƒ์ด์—์š”.

 

        ๐Ÿ“ฆ UserController.java

/**
 * ํšŒ์› ๊ด€๋ จ Controller
 * <pre>
 * <b>History:</b>
 *    ์ฃผ๋‹ˆํ•˜๋ž‘, 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * </pre>
 *
 * @author ์ฃผ๋‹ˆํ•˜๋ž‘
 * @version 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * @See ""
 * @see <a href=""></a>
 */

@Api(tags = {"ํšŒ์› ๊ด€๋ฆฌ ๊ด€๋ จ API"}) @Slf4j @RequiredArgsConstructor
@RequestMapping(ServiceURIMng.USER)
@RestController public class UserController {

    private final UserService userService;

    @ApiOperation(value = SwaggerApiInfo.DUPLICATE_ID, notes = "ํšŒ์› ๊ฐ€์ž… ์ „ ID ์ค‘๋ณต ํ™•์ธ ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.")
    @ApiParam(name = "DuplicateByIdInfoDTO", value = "์ด์šฉ์„ ์›ํ•˜๋Š” ์ด์šฉ์ž๊ฐ€ ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด ID ์ค‘๋ณต ํ™•์ธ ํ•ฉ๋‹ˆ๋‹ค.", readOnly = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "์‚ฌ์šฉ ๊ฐ€๋Šฅ"),
            @ApiResponse(code = 409, message = "์ž…๋ ฅ๊ฐ’ ์ค‘๋ณต"),
            @ApiResponse(code = 500, message = "Server Internal Error")
    })

    @ResponseBody
    @PostMapping("/duplicate/userid") public DefaultResponse duplicateUserID(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO) {

        log.info("UserController์˜ duplicateUserID(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ ํ™•์ธ : " + duplicateByIdInfoDTO.toString());
        log.info("userService.duplicateUserID(duplicateByIdInfoDTO)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

        return userService.duplicateUserID(duplicateByIdInfoDTO);

    } // duplicateId(@RequestBody String username) ๋

    @ApiOperation(value = SwaggerApiInfo.DUPLICATE_NICKNAME, notes = "ํšŒ์› ๊ฐ€์ž… ์ „ ๋ณ„๋ช… ์ค‘๋ณต ํ™•์ธ ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.")
    @ApiParam(name = "DuplicateByIdInfoDTO", value = "์ด์šฉ์„ ์›ํ•˜๋Š” ์ด์šฉ์ž๊ฐ€ ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด ๋ณ„๋ช… ์ค‘๋ณต ํ™•์ธ ํ•ฉ๋‹ˆ๋‹ค.", readOnly = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "์‚ฌ์šฉ ๊ฐ€๋Šฅ"),
            @ApiResponse(code = 409, message = "์ž…๋ ฅ๊ฐ’ ์ค‘๋ณต"),
            @ApiResponse(code = 500, message = "Server Internal Error")
    })

    @ResponseBody
    @PostMapping("/duplicate/nickname") public DefaultResponse duplicateUserNickName(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO) {

        log.info("UserController์˜ duplicateUserNickName(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ ํ™•์ธ : " + duplicateByIdInfoDTO.toString());
        log.info("userService.duplicateUserNickName(duplicateByIdInfoDTO)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

        return userService.duplicateUserNickName(duplicateByIdInfoDTO);

    } // duplicateId(@RequestBody String username) ๋

    @ApiOperation(value = SwaggerApiInfo.DUPLICATE_EMAIL, notes = "ํšŒ์› ๊ฐ€์ž… ์ „ E-mail ์ค‘๋ณต ํ™•์ธ ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.")
    @ApiParam(name = "DuplicateByIdInfoDTO", value = "์ด์šฉ์„ ์›ํ•˜๋Š” ์ด์šฉ์ž๊ฐ€ ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด mail ์ค‘๋ณต ํ™•์ธ ํ•ฉ๋‹ˆ๋‹ค.", readOnly = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "์‚ฌ์šฉ ๊ฐ€๋Šฅ"),
            @ApiResponse(code = 409, message = "์ž…๋ ฅ๊ฐ’ ์ค‘๋ณต"),
            @ApiResponse(code = 500, message = "Server Internal Error")
    })

    @ResponseBody
    @PostMapping("/duplicate/email") public DefaultResponse duplicateUserEmail(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO) {

        log.info("UserController์˜ duplicateUserEmail(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ ํ™•์ธ : " + duplicateByIdInfoDTO.toString());
        log.info("userService.duplicateUserEmail(duplicateByIdInfoDTO)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

        return userService.duplicateUserEmail(duplicateByIdInfoDTO);

    } // duplicateId(@RequestBody String username) ๋

    @ApiOperation(value = SwaggerApiInfo.DUPLICATE_PHONE_NUMBER, notes = "ํšŒ์› ๊ฐ€์ž… ์ „ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ค‘๋ณต ํ™•์ธ ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.")
    @ApiParam(name = "DuplicateByIdInfoDTO", value = "์ด์šฉ์„ ์›ํ•˜๋Š” ์ด์šฉ์ž๊ฐ€ ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ค‘๋ณต ํ™•์ธ ํ•ฉ๋‹ˆ๋‹ค.", readOnly = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "์‚ฌ์šฉ ๊ฐ€๋Šฅ"),
            @ApiResponse(code = 409, message = "์ž…๋ ฅ๊ฐ’ ์ค‘๋ณต"),
            @ApiResponse(code = 500, message = "Server Internal Error")
    })

    @ResponseBody
    @PostMapping("/duplicate/phone-number") public DefaultResponse duplicateUserPhoneNumber(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO) {

        log.info("UserController์˜ duplicateUserPhoneNumber(@RequestBody DuplicateByIdInfoDTO duplicateByIdInfoDTO)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ ํ™•์ธ : " + duplicateByIdInfoDTO.toString());
        log.info("userService.duplicateUserPhoneNumber(duplicateByIdInfoDTO)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

        return userService.duplicateUserPhoneNumber(duplicateByIdInfoDTO);

    } // duplicateId(@RequestBody String username) ๋

    @ApiOperation(value = SwaggerApiInfo.SIGN_UP, notes = "ํšŒ์› ๊ฐ€์ž… ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.")
    @ApiParam(name = "customUserDetails", value = "์ด์šฉ์„ ์›ํ•˜๋Š” ์ด์šฉ์ž๊ฐ€ ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด ์ž‘์„ฑํ•œ ๋‚ด์šฉ์„ ๋‹ด์€ ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค. \n ํ•„์ˆ˜ : ์ž๊ธฐ ์†Œ๊ฐœ, ํ”„๋กœํ•„ ์‚ฌ์ง„์„ ์ œ์™ธํ•œ ๋ชจ๋“  ํ•ญ๋ชฉ \n ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ์•”ํ˜ธํ™” ์ฒ˜๋ฆฌ", readOnly = true)
    @ApiResponses(value = {
            @ApiResponse(code=200, message = "Email ์ค‘๋ณต"),
            @ApiResponse(code=201, message = "๊ฐ€์ž… ์„ฑ๊ณต"),
            @ApiResponse(code = 500, message = "Server Internal Error")
    })

    @PostMapping("/signup") public ResponseEntity<DefaultResponse> signUp (@Valid @RequestBody CustomUserDetails customUserDetails) {

        log.info("UserController์˜ signUp (@Valid @RequestBody UserSignUpRequestDTO userSignUpRequestDTO)์ด ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");
        log.info("userService.signUp(userSignUpRequestDTO)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

        return new ResponseEntity<>(userService.signUp(customUserDetails), HttpStatus.OK);

    } // signUp (@Valid @RequestBody UserSignUpRequestDTO userSignUpRequestDTO) ๋

} // class ๋

 

๋ณธ๊ฒฉ์ ์œผ๋กœ Controller๋ฅผ ๋ถ„์„ํ•ด ๋ณผ ๊ฒƒ์ด์—์š”. ์ผ๋‹จ, `signUp()`์„ ์ œ์™ธํ•œ ๋ชจ๋“  Method๋Š” ๊ฐ ๊ฐ’์˜ ์ค‘๋ณต ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ Method์ธ ๊ฒƒ์ด์—์š”. ๋”ฐ๋ผ์„œ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” `ID ์ค‘๋ณต ํ™•์ธ`์— ๋Œ€ํ•œ ๊ฒƒ๋งŒ ๊ธ€์„ ์ž‘์„ฑ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

 

โ–บ duplicateUserID()

Swagger
Swagger

 

 

UserController.java - 31 ~ 53

์ตœ์ดˆ Swagger๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น API๊ฐ€ ์–ด๋–ค API์ธ์ง€ ํ‘œ์‹œ๋ฅผ ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋’ค `System.out.println()` ๋Œ€์‹  Console์— ๋™์ž‘๋“ค์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ถœ๋ ฅํ•œ `@Slf4j`๋ฅผ ์„ ์–ธํ•˜๊ณ , final Member ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด `@RequiredArgsConstructor`๋ฅผ ์„ ์–ธํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

 

๊ทธ๋Ÿฐ ๋‹ค์Œ Service๋ฅผ DIํ•˜๊ธฐ ์œ„ํ•ด Member ๋ณ€์ˆ˜๋กœ ์„ ์–ธ์„ ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

 

`@ResponseBody`๋ฅผ ํ†ตํ•ด JSON Type์˜ Data๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ ์–ธ ํ•ด ์ฃผ๊ณ , `@PostMapping`์œผ๋กœ HTTP Method `POST`๋ฅผ ํ†ตํ•ด Data๋ฅผ ๋ฐ›๊ฒ ๋‹ค๊ณ  ์„ ์–ธํ•˜๋ฉด์„œ ์—ฐ๊ฒฐ ๋  URI๋ฅผ ์„ค์ • ํ•ด ์ค€ ๊ฒƒ์ด์—์š”. `DefaultResponse`๋Š” ์ฃผ๋‹ˆํ•˜๋ž‘์ด ๋งŒ๋“  ๊ฐ์ฒด์ธ๋ฐ, ์‚ดํŽด ๋ณด์ž๋ฉด

 

/**
 * ์‘๋‹ต Message๋ฅผ ์œ„ํ•œ Class
 * <pre>
 * <b>History:</b>
 *    ์ฃผ๋‹ˆํ•˜๋ž‘, 1.0.0, 2022.02.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * </pre>
 *
 * @author ์ฃผ๋‹ˆํ•˜๋ž‘
 * @version 1.0.0, 2022.02.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * @See ""
 * @see <a href=""></a>
 */

@Data @AllArgsConstructor @Builder
public class DefaultResponse<T> {

    // API ์ƒํƒœ ์ฝ”๋“œ
    private Integer statusCode;

    // API ๋ถ€๊ฐ€ ์„ค๋ช… (ํ•œ๊ธ€)
    private String MessageKo;

    // API ๋ถ€๊ฐ€ ์„ค๋ช… (์˜์–ด)
    private String MessageEn;

    // API ์‘๋‹ต ๋ฐ์ดํ„ฐ
    private T data;

    // ์ƒํƒœ ์ฝ”๋“œ + ๋ถ€๊ฐ€ ์„ค๋ช… ๋ฐ˜ํ™˜
    public static <T> DefaultResponse<T> response(final Integer statusCode, final String MessageKo, final String MessageEn){
        return (DefaultResponse<T>) DefaultResponse.builder()
                .statusCode(statusCode)
                .MessageKo(MessageKo)
                .MessageEn(MessageEn)
                .build();
    }   // response(final Integer statusCode, final String MessageKo, final String MessageEn) ๋

    // ์ƒํƒœ ์ฝ”๋“œ + ๋ถ€๊ฐ€ ์„ค๋ช… + ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
    public static <T> DefaultResponse<T> response(final Integer statusCode, final String MessageKo, final String MessageEn, final T data){
        return (DefaultResponse<T>) DefaultResponse.builder()
                .statusCode(statusCode)
                .MessageKo(MessageKo)
                .MessageEn(MessageEn)
                .data(data)
                .build();
    }   // response(final Integer statusCode, final String MessageKo, final String MessageEn, final T data) ๋
} // class ๋
๋ฐ˜์‘ํ˜•

๋ฐ”๋กœ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ Class์ธ ๊ฒƒ์ด์—์š”.

์ด Class๋ฅผ ์‘๋‹ต์œผ๋กœ ๋˜์ ธ์ค€๋‹ค๋Š” ๊ฒƒ์€ ์‘๋‹ต Code, ์‘๋‹ต ๋ถ€๊ฐ€ ์„ค๋ช…(ํ•œ๊ธ€), ์‘๋‹ต ๋ถ€๊ฐ€ ์„ค๋ช…(์˜์–ด), API ์‘๋‹ต Data๋ฅผ ๊ฐ์ฒด๋กœ Client์—๊ฒŒ ์ „๋‹ฌ ํ•ด ์ฃผ๊ธฐ ์œ„ํ•œ Class์ธ ๊ฒƒ์ด์—์š”.

์ฃผ๋‹ˆํ•˜๋ž‘์€ ์ด Method์—์„œ ์š”์ฒญ์„ `@RequestBody`๋ฅผ ํ†ตํ•ด  `DuplicateByIdInfoDTO`๋กœ ๋ฐ›๊ธฐ๋กœ ํ•œ ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด Method๊ฐ€ ๋™์ž‘ํ–ˆ๋Š”์ง€ ๋ฌด์—‡์„ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ Console์— ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜์„œ ๋ฐ”๋กœ Service์— ํ•ด๋‹น Method๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, `DuplicateByIdInfoDTO`๋ฅผ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด์„œ ๋ฐ˜ํ™˜๊ฐ’์ด ์˜ค๋ฉด ๋ฐ”๋กœ ๋ฐ˜ํ™˜๋˜๋„๋ก ํ•œ ๊ฒƒ์ด์—์š”.

 

 

โ–บ signUp()

UserController.java - 117 ~ 112

์œ„ ๊ทธ๋ฆผ์—์„œ ๋‚˜์˜ค๋Š” Method๋Š” ํšŒ์› ๊ฐ€์ž…์„ ์ฒ˜๋ฆฌ ํ•ด ์ฃผ๋Š” Method์ธ ๊ฒƒ์ด์—์š”. 

`@Valid`๋ฅผ ํ†ตํ•ด ์š”์ฒญ๊ฐ’์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ฒŒ ํ•˜๊ณ , ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ `CustomUserDetails` ๊ฐ์ฒด๋ฅผ ๋ฐ›๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ด Mehotd๋Š” ๋ฐ˜ํ™˜ํ•  ๋•Œ, `ResponseEntity<DefaultResponse>`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ์–ด์„œ 122๋ฒˆ์งธ ์ค„์—์„œ ResponseEntity ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , Service์— ํ•ด๋‹น Method์— ๊ฒฐ๊ณผ๊ฐ’์ด ์˜ค๋ฉด ๋ฐ˜ํ™˜์„ ํ•ด ์ฃผ๋Š”๋ฐ, ์ž‘์—…์ด ์ •์ƒ์ ์œผ๋กœ ์ง„ํ–‰์ด ๋˜๋ฉด ๋ฐ˜ํ™˜์ด ์˜ค๊ธฐ ๋•Œ๋ฌธ์— HTTP Status Code 200(HTTpStatus.OK)๋ฅผ ๊ฐ™์ด ๋ฐ˜ํ™˜ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

โ€‹

 

 

        ๐Ÿ“ฆ UserService.java

/**
 * ํšŒ์› ๊ฐ€์ž… ๊ด€๋ จ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง
 * <pre>
 * <b>History:</b>
 *    ์ฃผ๋‹ˆํ•˜๋ž‘, 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 *    ์ฃผ๋‹ˆํ•˜๋ž‘, 1.0.1, 2022.03.24 ID ์ค‘๋ณต ํ™•์ธ Logic ๊ตฌํ˜„
 * </pre>
 *
 * @author ์ฃผ๋‹ˆํ•˜๋ž‘
 * @version 1.0.1, 2022.03.24 ID ์ค‘๋ณต ํ™•์ธ Logic ๊ตฌํ˜„
 * @See ""
 * @see <a href=""></a>
 */

public interface UserService {

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    DefaultResponse duplicateUserID(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    DefaultResponse duplicateUserNickName(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    DefaultResponse duplicateUserEmail(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    DefaultResponse duplicateUserPhoneNumber(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์„œ๋น„์Šค
     * @param customUserDetails - ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๋‹ด์€ Object
     * @return DefaultResponse - ์„œ๋ฒ„ ์ฒ˜๋ฆฌ ์—ฌ๋ถ€์— ํ•ด๋‹นํ•˜๋Š” Status Code ๋ฐ˜ํ™˜์„ ์œ„ํ•œ ๊ฐ์ฒด
     * @see ""
     */

    DefaultResponse signUp(CustomUserDetails customUserDetails);


} // interface ๋

 

UserService๋Š” ๋‹จ์ˆœํžˆ ์ถ”์ƒ Method๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ฐ Logic์— ๋งž๊ฒŒ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด์—์š”.

 

 

        ๐Ÿ“ฆ UserServiceImpl.java

/**
 * ํšŒ์› ๊ฐ€์ž… ๊ด€๋ จ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง
 * <pre>
 * <b>History:</b>
 *    ์ฃผ๋‹ˆํ•˜๋ž‘, 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * </pre>
 *
 * @author ์ฃผ๋‹ˆํ•˜๋ž‘
 * @version 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * @See ""
 * @see <a href=""></a>
 */

@RequiredArgsConstructor @Slf4j
@Service public class UserServiceImpl implements UserService {

    private final PasswordEncoder passwordEncoder;
    private final UserMapper userMapper;

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ID ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    @Override
    public DefaultResponse duplicateUserID(DuplicateByIdInfoDTO duplicateByIdInfoDTO) {

        log.info("UserServiceImpl์˜ duplicateUserID(String username)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ : " + duplicateByIdInfoDTO);
        log.info("DB์—์„œ ํšŒ์› ๊ฐ€์ž… ์š”์ฒญ ์ด์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ UserId(username)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ํšŒ์›์ด ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ๊ฒ ์Šต๋‹ˆ๋‹ค!");
        Optional<DuplicateByIdInfoDTO> findByUserID = userMapper.findByUserID(duplicateByIdInfoDTO);

        if (findByUserID.isEmpty()) {

            log.info("์ค‘๋ณต ๋˜๋Š” ๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค! 200 Code์™€ ํ•จ๊ป˜ \"์‚ฌ์šฉ ๊ฐ€๋Šฅ!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(200, "์‚ฌ์šฉ ๊ฐ€๋Šฅ", "OK", findByUserID);

        } else {

            log.info("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ID ์ž…๋‹ˆ๋‹ค! 409 Code์™€ ํ•จ๊ป˜ \"์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’ ์ž…๋‹ˆ๋‹ค!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

        return DefaultResponse.response(409, "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ID ์ž…๋‹ˆ๋‹ค!", "Conflict");
        } // // if-else (checkEmail.isEmpty()) ๋

    } // duplicateId(String username) ๋

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ๋ณ„๋ช… ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    @Override
    public DefaultResponse duplicateUserNickName(DuplicateByIdInfoDTO duplicateByIdInfoDTO) {
        log.info("UserServiceImpl์˜ duplicateUserID(String username)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ : " + duplicateByIdInfoDTO);
        log.info("DB์—์„œ ํšŒ์› ๊ฐ€์ž… ์š”์ฒญ ์ด์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ณ„๋ช…์„ ํ†ตํ•ด ํ•ด๋‹น ํšŒ์›์ด ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ๊ฒ ์Šต๋‹ˆ๋‹ค!");
        Optional<DuplicateByIdInfoDTO> findByUserNickName = userMapper.findByUserNickName(duplicateByIdInfoDTO);



        if (findByUserNickName.isEmpty()) {

            log.info("์ค‘๋ณต ๋˜๋Š” ๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค! 200 Code์™€ ํ•จ๊ป˜ \"์‚ฌ์šฉ ๊ฐ€๋Šฅ!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(200, "์‚ฌ์šฉ ๊ฐ€๋Šฅ", "OK", findByUserNickName);

        } else {

            log.info("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ณ„๋ช… ์ž…๋‹ˆ๋‹ค! 409 Code์™€ ํ•จ๊ป˜ \"์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’ ์ž…๋‹ˆ๋‹ค!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(409, "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ณ„๋ช… ์ž…๋‹ˆ๋‹ค!", "Conflict");
        } // // if-else (checkEmail.isEmpty()) ๋
    }

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ Email ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    @Override
    public DefaultResponse duplicateUserEmail(DuplicateByIdInfoDTO duplicateByIdInfoDTO) {
        log.info("UserServiceImpl์˜ duplicateUserID(String username)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ : " + duplicateByIdInfoDTO);
        log.info("DB์—์„œ ํšŒ์› ๊ฐ€์ž… ์š”์ฒญ ์ด์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ UserId(username)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ํšŒ์›์ด ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ๊ฒ ์Šต๋‹ˆ๋‹ค!");
        Optional<DuplicateByIdInfoDTO> findByUserEmail = userMapper.findByUserEmail(duplicateByIdInfoDTO);

        if (findByUserEmail.isEmpty()) {

            log.info("์ค‘๋ณต ๋˜๋Š” ๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค! 200 Code์™€ ํ•จ๊ป˜ \"์‚ฌ์šฉ ๊ฐ€๋Šฅ!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(200, "์‚ฌ์šฉ ๊ฐ€๋Šฅ", "OK", findByUserEmail);

        } else {

            log.info("์ด๋ฏธ ์กด์žฌํ•˜๋Š” Email ์ž…๋‹ˆ๋‹ค! 409 Code์™€ ํ•จ๊ป˜ \"์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’ ์ž…๋‹ˆ๋‹ค!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(409, "์ด๋ฏธ ์กด์žฌํ•˜๋Š” Email ์ž…๋‹ˆ๋‹ค!", "Conflict");
        } // // if-else (checkEmail.isEmpty()) ๋
    }

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    @Override
    public DefaultResponse duplicateUserPhoneNumber(DuplicateByIdInfoDTO duplicateByIdInfoDTO) {
        log.info("UserServiceImpl์˜ duplicateUserID(String username)๊ฐ€ ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! Client๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ๊ฐ’ : " + duplicateByIdInfoDTO);
        log.info("DB์—์„œ ํšŒ์› ๊ฐ€์ž… ์š”์ฒญ ์ด์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ UserId(username)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ํšŒ์›์ด ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ๊ฒ ์Šต๋‹ˆ๋‹ค!");
        Optional<DuplicateByIdInfoDTO> findByUserphoneNumber = userMapper.findByUserPhoneNumber(duplicateByIdInfoDTO);

        if (findByUserphoneNumber.isEmpty()) {

            log.info("์ค‘๋ณต ๋˜๋Š” ๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค! 200 Code์™€ ํ•จ๊ป˜ \"์‚ฌ์šฉ ๊ฐ€๋Šฅ!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(200, "์‚ฌ์šฉ ๊ฐ€๋Šฅ", "OK", findByUserphoneNumber);

        } else {

            log.info("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ž…๋‹ˆ๋‹ค! 409 Code์™€ ํ•จ๊ป˜ \"์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’ ์ž…๋‹ˆ๋‹ค!\" ๋ฐ˜ํ™˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");

            return DefaultResponse.response(409, "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ์ž…๋‹ˆ๋‹ค!", "Conflict");
        } // // if-else (checkEmail.isEmpty()) ๋
    }

    /**
     * ํšŒ์› ๊ฐ€์ž… ์„œ๋น„์Šค
     * @param customUserDetails - ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๋‹ด์€ Object
     * @return DefaultResponse - ์„œ๋ฒ„ ์ฒ˜๋ฆฌ ์—ฌ๋ถ€์— ํ•ด๋‹นํ•˜๋Š” Status Code ๋ฐ˜ํ™˜์„ ์œ„ํ•œ ๊ฐ์ฒด
     * @see ""
     */

    @Override
    public DefaultResponse signUp(CustomUserDetails customUserDetails) {

        log.info("UserServiceImpl์˜ signUp(CustomUserDetails customUserDetails)์ด ํ˜ธ์ถœ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");

        log.info("DB์—์„œ ํšŒ์› ๊ฐ€์ž… ์š”์ฒญ ์ด์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ UserId(username)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ํšŒ์›์ด ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ๊ฒ ์Šต๋‹ˆ๋‹ค!");

        log.info("์ด์šฉ์ž์˜ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์•”ํ˜ธํ™” ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!");
        customUserDetails.setPassword(passwordEncoder.encode(customUserDetails.getPassword()));

        userMapper.signUp(customUserDetails);

        return DefaultResponse.response(200, "ํšŒ์› ๊ฐ€์ž… ์™„๋ฃŒ!", "Success Sign up");
    } // signUp(CustomUserDetails customUserDetails) ๋
} // class ๋

 

 

 

        ๐Ÿ“ฆ UserMapper.java

/**
 * ํšŒ์› ๊ด€๋ จ Mapper
 * <pre>
 * <b>History:</b>
 *    ์ฃผ๋‹ˆํ•˜๋ž‘, 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * </pre>
 *
 * @author ์ฃผ๋‹ˆํ•˜๋ž‘
 * @version 1.0.0, 2022.03.24 ์ตœ์ดˆ ์ž‘์„ฑ
 * @See ""
 * @see <a href=""></a>
 */

@Mapper @Repository public interface UserMapper {

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    Optional<DuplicateByIdInfoDTO> findByUserID(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ - ๋ณ„๋ช…
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    Optional<DuplicateByIdInfoDTO> findByUserNickName(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ - E-mail
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    Optional<DuplicateByIdInfoDTO> findByUserEmail(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก๋œ ์ •๋ณด ์ธ์ง€ ํ™•์ธ - ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ
     * @param duplicateByIdInfoDTO - ํšŒ์› ๊ฐ€์ž… ์ „ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋Š” ์ •๋ณด ์ธ์ง€ ํ™•์ธ์„ ์œ„ํ•œ ์ด์šฉ์ž ์ž…๋ ฅ ์ •๋ณด DTO
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    Optional<DuplicateByIdInfoDTO> findByUserPhoneNumber(DuplicateByIdInfoDTO duplicateByIdInfoDTO);

    /**
     * ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•œ ์ถ”์ƒ Method
     * @param customUserDetails - ํšŒ์› ๊ฐ€์ž… ์‹œ ์ด์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด ๋‹ด์€ ๊ฐ์ฒด
     * @return CustomUserDetails - ํ•ด๋‹น ํšŒ์›์˜ ์ •๋ณด ๋ฐ˜ํ™˜
     * @see ""
     */

    void signUp(CustomUserDetails customUserDetails);
} // interface ๋

 

โ–บ duplicateUserID()

UserServiceImpl.java - 27 ~ 60

์ด ์นœ๊ตฌ๊ฐ€ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋น„์ฆˆ๋‹ˆ์Šค Logic์„ ๋‹ด๋‹นํ•˜๊ฒŒ ๋  UserService๋ฅผ Implements(๊ตฌํ˜„)ํ•œ ๊ตฌํ˜„์ฒด์ธ ๊ฒƒ์ด์—์š”.

๋จผ์ € ํ•ด๋‹น Class๋ฅผ Spring Bean์— ํ•ด๋‹น Class๋ฅผ Service๋กœ ์ž๋™ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•ด `@Service`๋ฅผ ์„ ์–ธํ•ด ์ค€ ๊ฒƒ์ด์—์š”. 

๊ทธ๋Ÿฐ ๋‹ค์Œ ํšŒ์› ๊ฐ€์ž…์„ ํ•  ๋•Œ, ์ด์šฉ์ž์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™” ํ•˜๊ธฐ ์œ„ํ•ด `PasswordEncoder` ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๊ณ , DB๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด `UserMapper `๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•œ ๊ฒƒ์ด์—์š”. ์ด ์นœ๊ตฌ๋“ค์€ final๋กœ ์ •์˜๋˜์–ด ์žˆ๊ณ , ์ด ๋•Œ๋ฌธ์— `@RequiredArgsConstructor`๋ฅผ ์„ ์–ธํ•˜์—ฌ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

์ตœ์ดˆ ์ด Method๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด `Mapper`์— ํ•ด๋‹น Method๋ฅผ ํ†ตํ•ด `DuplicateByIdInfoDTO`๋ฅผ ์ „๋‹ฌํ•ด์„œ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ด์šฉ์ž๊ฐ€ ID ์ค‘๋ณต ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ID๋ฅผ DB์— Selectํ•˜๊ฒŒ ๋˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ ๊ฐ’์„ `Optional`๋กœ `NPE(Null Point Exception)` ๋ฐฉ์ง€๋ฅผ ํ†ตํ•ด `DuplicateByIdInfoDTO` ๊ฐ์ฒด ์ž๋ฃŒํ˜• Type์˜ ๋ณ€์ˆ˜ `findByuserID`์— ๋‹ด์•„ ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋’ค 47๋ฒˆ์งธ ์ค„์— if๋ฌธ์„ ํ†ตํ•ด ํ•ด๋‹น ๋ณ€์ˆ˜์— ๊ฐ’์ด ๋น„์–ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต๋˜๋Š” ID๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜ ํ•  ๋•Œ, HTTP Status Code 200๊ณผ ํ•จ๊ป˜ ์‘๋‹ต Message๋กœ ํ•œ๊ธ€์€ `์‚ฌ์šฉ ๊ฐ€๋Šฅ`๊ณผ ์˜์–ด๋กœ๋Š” `OK`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ํ•ด๋‹น ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€๋กœ ๋ฐ˜ํ™˜ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”. 

๋งŒ์•ฝ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ID๋Š” ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋˜๊ณ , ๋ฐ˜ํ™˜ ํ•  ๋•Œ, HTTP Status Code๋ฅผ 409์™€ ํ•จ๊ป˜ ์‘๋‹ต Message๋กœ ํ•œ๊ธ€์€ `์ด๋ฏธ ์กด์žฌํ•˜๋Š” ID ์ž…๋‹ˆ๋‹ค!`์™€ ์˜์–ด๋กœ `Confilct`๋ฅผ ๋ฐ˜ํ™˜ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

โ–บ signUp()

UserServiceImpl.java - 145 ~ 165

ํšŒ์› ๊ฐ€์ž…์— ๋Œ€ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ ์ด ๊ณณ์—์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

์ตœ์ดˆ ์ด ์นœ๊ตฌ๊ฐ€ ๋™์ž‘์„ ํ•˜๊ฒŒ ๋˜๋ฉด ์ด์šฉ์ž ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™” ํ•˜๊ธฐ ์œ„ํ•ด `PasswordEncoder`์— `encode()`๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ด์šฉ์ž๊ฐ€ ๋ณด๋‚ธ `CustomUserDetails` ๊ฐ์ฒด ์•ˆ์— ์ž…๋ ฅ๋œ password๋ผ๋Š” Member ๋ณ€์ˆ˜์— ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ๊ทธ ๊ฐ’์„ ์•”ํ˜ธํ™” ํ•˜๊ณ , ๊ทธ๊ฒƒ์„ ๋‹ค์‹œ setter๋ฅผ ํ†ตํ•ด ์ €์žฅํ•ด ๋‘๋Š” ๊ฒƒ์ด์—์š”.

์ฐธ๊ณ ๋กœ ๋‹จ์ˆœํžˆ ์ด๋ ‡๊ฒŒ `PasswordEncoder`๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด Exception์ด ๋œจ๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Š” `PasswordEncoder`๊ฐ€ ํ•˜๋Š” ์—ญํ• ์€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌํ˜„์ฒด๋“ค์ด ํ•˜๋Š” ์—ญํ• ์ด ๋ฐ”๋กœ ์ด ์•”ํ˜ธํ™”๋ฅผ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํ•  ๊ฒƒ์ธ์ง€ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์ •์˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ด ๋•Œ๋ฌธ์— `PasswordEncoder`์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋Œ€์ž…ํ•ด์ฃผ๊ณ , ์ด๋ฅผ Spring Bean์— ๋“ฑ๋กํ•˜๋Š” ๊ณผ์ •์ด ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ,

์œ„์— Code๋กœ๋งŒ ๋ณด์•˜์„ ๋•Œ๋Š” ์ด ๊ณผ์ •์ด ์ƒ๋žต๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

`Spring Security`์— ๋Œ€ํ•ด ํ•„์š”ํ•œ ์„ค์ •์„ ํ•˜๋Š” Config ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์œ„์— ์„ค์ •๋“ค์„ ํ•ด ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค.
Config ๊ฐ์ฒด๋Š” `WebSecurityConfigurerAdapter`๋ฅผ ์ƒ์† ๋ฐ›์•„ `configure()`๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

SecurityConfig.java

์ฃผ๋‹ˆํ•˜๋ž‘์€ `Spring Security` Config ๊ฐ์ฒด๋ฅผ ์œ„์™€ ๊ฐ™์ด ์ •์˜๋ฅผ ํ•œ ๊ฒƒ์ด์—์š”.

์ตœ์ดˆ 28๋ฒˆ์งธ ์ค„์— `@Configuration`์„ ํ†ตํ•ด ํ•ด๋‹น Class๊ฐ€ 1๊ฐœ ์ด์ƒ์˜ Bean์„ ์ œ๊ณตํ•œ๋‹ค๊ณ  ์•Œ๋ ค ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋‹ค์Œ 29๋ฒˆ์งธ ์ค„์— `@EnableWebSecurity`๋ฅผ ํ†ตํ•ด `Spring Security`๊ฐ€ ์ž‘๋™ํ•˜๋„๋ก ์ž‘๋™ Button์„ ๋ˆŒ๋Ÿฌ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

30๋ฒˆ์งธ ์ค„์— `@EnableGlobalMethodSecurity(prePostEnabled = true)`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š”๋ฐ, ํŠน์ • Method๊ฐ€ ํ˜ธ์ถœ ๋œ ์ด ์ „๊ณผ ์ด ํ›„ ํ˜ธ์ถœ์„ ์š”์ฒญํ•œ ์ด์šฉ์ž์˜ ๊ถŒํ•œ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ธ ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋’ค `@Bean`์„ ํ†ตํ•ด `Srping Framwork`์—์„œ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ Bean์œผ๋กœ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ ์–ธํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

์ฆ‰, `@Autowired`๋ฅผ ์ด์šฉํ•˜์—ฌ `Spring Framwork`์—์„œ `DI(Dependncy Injection)์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์—์š”. ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ์™ธ๋ถ€ Class์—์„œ `PasswordEncoder`๋ฅผ `@Autowird`๋ฅผ ์ด์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ `DI`ํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!

์ฐธ๊ณ ๋กœ ์ฃผ๋‹ˆํ•˜๋ž‘์€ `@Autowird` ๋Œ€์‹  `@RequiredArgsConstructor`๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์ด์—์š”.

 

 

UserServiceImpl.java - 145 ~ 165

๋‹ค์‹œ ์ด ๊ณณ์œผ๋กœ ๋Œ์•„์™€์„œ 162๋ฒˆ์งธ ์ค„์— userMappe์— ํ•ด๋‹น Method๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, `CustomUserDetails` ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฐ ๋’ค Controller์—๊ฒŒ ๋ฐ˜ํ™˜ํ•  ๋•Œ, HTTP Status Code 200 ์ˆซ์ž์™€ ํ•จ๊ป˜, ํ•œ๊ธ€ Message ํšŒ์› ๊ฐ€์ž… ์™„๋ฃŒ!์™€ ์˜์–ด Success Sign up์„ ์ „๋‹ฌ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

 

        ๐Ÿ“ฆ UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.devcommunity.junyharang.mapper.user.UserMapper">

    <!-- ๊ธฐ ๋“ฑ๋ก๋œ ํšŒ์› ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ID -->
    <select id="findByUserID" parameterType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO"
            resultType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO">

        select username
        from user
        where username = #{username}
    </select>

    <!-- ๊ธฐ ๋“ฑ๋ก๋œ ํšŒ์› ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ๋ณ„๋ช… -->
    <select id="findByUserNickName" parameterType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO"
            resultType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO">

        select nickname
        from user
        where nickname = #{nickname}
    </select>

    <!-- ๊ธฐ ๋“ฑ๋ก๋œ ํšŒ์› ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ Email -->
    <select id="findByUserEmail" parameterType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO"
            resultType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO">

        select userEmail
        from user
        where userEmail = #{userEmail}
    </select>

    <!-- ๊ธฐ ๋“ฑ๋ก๋œ ํšŒ์› ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ -->
    <select id="findByUserPhoneNumber" parameterType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO"
            resultType="com.devcommunity.junyharang.model.dto.user.DuplicateByIdInfoDTO">

        select userPhone
        from user
        where userPhone = #{userPhone}
    </select>


    <!-- ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด DB์— ํšŒ์› ์ •๋ณด ์ €์žฅ -->
    <insert id="signUp" parameterType="com.devcommunity.junyharang.model.vo.member.CustomUserDetails">
        <![CDATA[
        insert into user (
            username,
            password,
            nickname,
            userEmail,
            userPhone,
            enable,
            authority
        ) values (
          #{username},
          #{password},
          #{nickname},
          #{userEmail},
          #{userPhone},
          1,
          "ROLE_USER"
         )
        ]]>
    </insert>
</mapper>

 

โ–บ duplicateUserID()

UserMapper.xml - 5 ~ 12

ํ•ด๋‹น ๊ธฐ๋Šฅ์€ ID ์ค‘๋ณต ํ™•์ธ์ธ ๊ฒƒ์ด์—์š”. `Service Layer`์—์„œ ํ•ด๋‹น Mapper๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์–ป๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ด์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ID๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ๊ฒƒ์ด์—์š”.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— SQL์— `SELECT`๋ฅผ ํ†ตํ•ด username Column์— ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ ๋œ DTO์•ˆ์— ์žˆ์„ username์ด ์žˆ๋Š”์ง€ ์ฐพ๋„๋ก ํ•˜๊ณ , ์ด๋ฅผ ๋‹ค์‹œ `DuplicateByIdInfoDTO`์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•œ ๊ฒƒ์ด์—์š”.

 

 

โ–บ signUp()

UserMapper.xml - 42 ~ 63

ํ•ด๋‹น ๊ธฐ๋Šฅ์€ ๋ณธ๊ฒฉ์ ์ธ ํšŒ์› ๊ฐ€์ž…์„ ์œ„ํ•ด DB์— ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์ด์—์š”.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— SQL์— `INSERT`๋ฅผ ํ†ตํ•ด ํšŒ์› ๊ฐ€์ž…์„ ์‹œ๋„ํ•˜๋Š” ์ด์šฉ์ž๊ฐ€ ๋ณด๋‚ธ ๊ฐ’์„ ๊ฐ Column์— ์ž…๋ ฅํ•˜์—ฌ ์ €์žฅ ๋˜๊ฒŒ ํ•˜๊ณ , ํ•ด๋‹น ๊ณ„์ •์ด ํ™œ์„ฑํ™” ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด `enable` Column์— `1` ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ , ํ•ด๋‹น ์ด์šฉ์ž์˜ ๊ธฐ๋ณธ ๊ถŒํ•œ์„ `ROLE_USER`๋กœ ๋„ฃ์–ด์ค€ ๊ฒƒ์ด์—์š”.

 

 

 

๐Ÿš€ ๊ฒฐ๊ณผ

User Table Value

 

    ๐Ÿ”ฝ  ID ์ค‘๋ณต ํ™•์ธ

        ๐Ÿ“ฆ ๋“ฑ๋ก๋˜์ง€ ์•Š์€ ID

 

 

 

        ๐Ÿ“ฆ ๋“ฑ๋ก๋œ ID

 

 

    ๐Ÿ”ฝ  ํšŒ์› ๊ฐ€์ž…

 

 

์งœ์ž”! ๐Ÿ˜ƒ

์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

๐Ÿš€ ์ˆ˜์ • ์‚ฌํ•ญ - Validation

์œ„์˜ ๊ธ€์—์„œ๋Š” ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ (Validation)์ด ์ „ํ˜€ ์ง„ํ–‰๋˜๊ณ  ์žˆ์ง€ ์•Š์€ ๊ฒƒ์ด์—์š”.
์—ฌ๊ธฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ '์ด ๊ณณ'์— ์ค€๋น„ ํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค!

 

 

    ๐Ÿ”ฝ  ํšŒ์› ID ์ค‘๋ณต ํ™•์ธ - ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

DuplicateByUserIDDTO.java

 

 

UserController.java - duplicateUserID()

 

 

        ๐Ÿ“ฆ ๊ณต๋ฐฑ ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

 

 

 

        ๐Ÿ“ฆ ์•„๋ฌด ๋‚ด์šฉ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

 

 

 

        ๐Ÿ“ฆ Null ์ž…๋ ฅ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

 

 

 

        ๐Ÿ“ฆ ํŠน์ˆ˜ ๋ฌธ์ž ์ž…๋ ฅ ์‹œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

 

 

์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋Š” ๋ชจ๋“  ์ค‘๋ณต ํ™•์ธ ๋ฐ ํšŒ์› ๊ฐ€์ž…์— ์ถ”๊ฐ€๋ฅผ ํ•ด ๋‘” ๊ฒƒ์ด์—์š”.

์ž์„ธํ•œ ์„ค๋ช…์€ ๋”ฐ๋กœ ๋‚ด์šฉ์„ ์ •๋ฆฌ ํ•ด ๋‘์—ˆ์œผ๋‹ˆ ํšŒ์› ๊ฐ€์ž…์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ์„œ ๋งˆ์น˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•