본문 바로가기

Spring Boot

글쓰기/주소 설계/@Autowired DI하는 방법3가지

User 관련(로그인,회원가입,로그아웃)부분을 이미 만들어놔서

어느 정도 복사와 붙여넣기로 빠르게 만들 수 있다.

 

 

 

 

saveForm은 joinFrom.jsp를 복붙해서 알맞게 고친다.

 


 

 

post.js는 user.js를 가져오며 알맞게 고친다.

그리고 model을 보면 userId가 필요하기 때문에 hidden값으로 principal에 담겨있던 id값을 가져온다. 

 


 

 

PostController에서 /post는 restAPI를 이용해서 주소를 만들때 사용하는 방법이다.

 

RestAPI주소 설계방법

Post

 

주소에 명사만 들어와요. 동사말고! 예)delete와 같은 동사는 안들어 옴.

 

Get       select 1건      /post/{id}

Get       select All       /posts

Post      insert           /post

Put       update         /post

Delete   delete 1건     /post/{id}

Delete   delete All      /posts

 

예를 들어

어떤 user가 들고 있는 상품을 한 건 확인 하고 싶을 때

Get      /user/1/product/2

 

유저5번이 들고 있는 상품1번을 삭제하고 싶어

Delete   /user/5/product/1

 

5번 상품을 들고있는 user

Get       /product/5/user


 

Repository에 save함수를 만들고

 

 

 

 

Service에서 로직 관련을 만든다.

 

@AutoWired하는 방법

@Controller

@RequiredArgsConstructor

public class PostController {

 

private final PostService postService; //이게 AutoWired다!

...

더보기
@Controller
@RequiredArgsConstructor
public class PostController {

	private final PostService postService; //이게 AutoWired다!

 

 

 

postMapper.xml로 연결시켜줌