package com.sparta.myBlogBackendServer.controller;
import com.sparta.myBlogBackendServer.dto.SignupRequestDto;
import com.sparta.myBlogBackendServer.entity.User;
import com.sparta.myBlogBackendServer.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
@PostMapping("/signup")
public User signup (@RequestBody SignupRequestDto signupRequestDto) {
User user = userService.signup(signupRequestDto);
return user;
}
}
package com.sparta.myBlogBackendServer.service;
import com.sparta.myBlogBackendServer.dto.SignupRequestDto;
import com.sparta.myBlogBackendServer.entity.User;
import com.sparta.myBlogBackendServer.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import java.util.regex.Pattern;
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
// 회원가입
@Transactional
public User signup(SignupRequestDto signupRequestDto) {
String username = signupRequestDto.getUsername();
String password = signupRequestDto.getPassword();
// 아이디 정규식 확인
if (!Pattern.matches("^[a-z0-9]{4,10}$", username)) {
throw new IllegalArgumentException("아이디는 4자 이상, 10자 이하 알파벳 소문자, 숫자로만 이루어져야 합니다.");
}
// 비밀번호 정규식 확인
if (!Pattern.matches("^\\w{8,15}$", password)) {
throw new IllegalArgumentException("비밀번호는 8자 이상, 15자 이하 알파벳 대/소문자, 숫자로만 이루어져야 합니다.");
}
// 회원 중복 확인
Optional<User> found = userRepository.findByUsername(username);
if (found.isPresent()) {
throw new IllegalArgumentException("중복된 아이디가 존재합니다.");
}
User user = new User(username, password);
userRepository.save(user);
return user;
}
}
package com.sparta.myBlogBackendServer.entity;
import javax.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Entity(name = "users")
@Getter
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
package com.sparta.myBlogBackendServer.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SignupRequestDto {
private String username;
private String password;
}
2022-12-16T16:52:59.399+09:00 ERROR 11128 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postController' defined in file [C:\Users\ychong\Desktop\sparta_java_spring\MyBlogBackendServer\build\classes\java\main\com\sparta\myBlogBackendServer\controller\PostController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'postService' defined in file [C:\Users\ychong\Desktop\sparta_java_spring\MyBlogBackendServer\build\classes\java\main\com\sparta\myBlogBackendServer\service\PostService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'postRepository' defined in com.sparta.myBlogBackendServer.repository.PostRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.sparta.myBlogBackendServer.entity.Post
2022-12-16T16:52:59.399+09:00 ERROR 11128 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postController' defined in file [C:\Users\ychong\Desktop\sparta_java_spring\MyBlogBackendServer\build\classes\java\main\com\sparta\myBlogBackendServer\controller\PostController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'postService' defined in file [C:\Users\ychong\Desktop\sparta_java_spring\MyBlogBackendServer\build\classes\java\main\com\sparta\myBlogBackendServer\service\PostService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'postRepository' defined in com.sparta.myBlogBackendServer.repository.PostRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.sparta.myBlogBackendServer.entity.Post
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.0'
id 'io.spring.dependency-management' version '1.1.0'
}
-> 스프링부트 버전을 낮추거나 import하는 스프링부트 버전을 높여야 함, 난 스프링부트 버전을 낮춤 (강의 내용이 javax 기준으로 진행되기 때문)
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.1.0'
}
-> build.gradle에서 자바 스프링부트 버전 3.0.0 (jakarta 가능)
-> 2.7.5로 변경하여 -> javax도 가능하게 함
@Entity(name = "users")
@Getter
@NoArgsConstructor
public class User {
if (!Pattern.matches("^[a-z][0-9]{4,10}$", username)) {
throw new IllegalArgumentException
("아이디는 4자 이상,
10자 이하 알파벳 소문자, 숫자로만 이루어져야 합니다.");
}
-> 정규식 표현 [0-9][a-z]가 아니라 [0-9a-z]로 표현해야 함
if (!Pattern.matches("^[a-z0-9]{4,10}$", username)) {
throw new IllegalArgumentException
("아이디는 4자 이상,
10자 이하 알파벳 소문자, 숫자로만 이루어져야 합니다.");
}
회원가입이 정상적으로 작동되는 걸 확인할 수 있었다!!(행벅ㅜㅜ)
오류 메시지 출력까지 확인~
아직 할 일이 많지만 이번 주말 불태워서 마무리 해야겠다.