[初心-Spring Boot] 게시판 제작 - 2. 회원가입 구현

1. Validation, Lombok 의존성 추가


file: gradle.build
plugins {
	id 'org.springframework.boot' version '2.5.2'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
}

group = 'com.rptp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//  =======================ADD=======================
 	implementation 'org.springframework.boot:spring-boot-starter-validation'
	annotationProcessor "org.projectlombok:lombok"
	compileOnly "org.projectlombok:lombok"
//  =======================ADD=======================
  
  
  
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
  
  
  
//  =======================ADD=======================
 	testCompileOnly 'org.projectlombok:lombok:1.18.20'
	testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
//  =======================ADD=======================
  
  
  
	runtimeOnly 'mysql:mysql-connector-java'
}

test {
	useJUnitPlatform()
}

2. Member core 생성

2-1. model 생성


file: Member.java
package com.rptp.rptpSpringBoot.core.member.domin;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long memberId;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String password;

    private String profilePhoto;

    @Column(nullable = false)
    private String nickName;

}

2-2. request 생성


file: SignUpRequest.java
@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
public class SignUpRequest {

    @NotBlank
    private String name;

    @NotBlank
    private String password;
    
    private String profilePhoto;

    private String nickName;
}

2-3. MemberRepository 생성


file: MemberRepository.java
package com.rptp.rptpSpringBoot.core.member.domain;

import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Long> {
    
}

2-4. MemberService 생성


file: MemberService.java
package com.rptp.rptpSpringBoot.core.member.service;

import com.rptp.rptpSpringBoot.core.member.domain.Member;
import com.rptp.rptpSpringBoot.core.member.domain.MemberRepository;
import com.rptp.rptpSpringBoot.core.member.dto.SignUpRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class MemberService {

    private final MemberRepository memberRepository;

    @Transactional
    public Long signUp(SignUpRequest req) {
        return  memberRepository.save(buildMember(req)).getMemberId();
    }

    private Member buildMember(SignUpRequest req) {
        return Member.builder()
                .name(req.getName())
                .password(req.getPassword())
                .profilePhoto(req.getProfilePhoto())
                .nickName(req.getNickName())
                .build();
    }

}

password 암호화는 후에 Spring sequrity에서 다룰 예정

3. Member api 생성

3-1. MemberController 생성


file: MemberController/java
package com.rptp.rptpSpringBoot.api;

import com.rptp.rptpSpringBoot.core.member.dto.SignUpRequest;
import com.rptp.rptpSpringBoot.core.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/api/member")
@RequiredArgsConstructor
public class MemberController {
    private final MemberService memberService;

    @PostMapping("")
    public String signUp(SignUpRequest req) {
        memberService.signUp(req);
        return "redirect:/sign-up-success";
    }

}

4. View 및 페이지 이동 Contorller 생성

4-1. MainController 생성


file: MainController.java
package com.rptp.rptpSpringBoot.api;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
@RequiredArgsConstructor
public class MainController {

    @GetMapping("")
    public String index(){
        return "/index";
    }

    @GetMapping("sign-up")
    public String signUp() {
        return "/sign-up";
    }
    
    @GetMapping("sign-up-success")
    public String signUpSuccess() {
        return "/sign-up-success";
    }
}

4-2. View page 생성


file: index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RPTP</title>
</head>
<body>
rptp 게시판
<a th:href="@{/sign-up}">회원가입</a>
</body>
</html>
file: sign-up.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RPTP</title>
</head>
<body>
<form th:action="@{/api/member}" method="post">
    <label>아이디<input type="text" name="name"></label>
    <label>password<input type="password" name="password"></label>
    <label>프로필사진<input type="text" name="profilePhoto"></label>
    <label>별명<input type="text" name="nickName"></label>
    <input type="submit" value="가입하기">
</form>

</body>
</html>
file: sign-up-success.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RPTP</title>
</head>
<body>
회원가입 완료!
<a th:href="@{/index}">메인으로</a>
</body>
</html>

5. 실행




좋은 웹페이지 즐겨찾기