이제 커피 주문 애플리케이션을 만들면서 Spring MVC에 대해 공부할 예정이다!
Spring 의 모듈 중에서 웹 계층을 담당하는 몇 가지 모듈이 있다. 특히 서블릿(Servlet) API를 기반으로 클라이언트의 요청을 처리하는 모듈이 있는데, 이 모듈 이름이 바로 spring-webmvc입니다. 이걸 Spring MVC 프레임워크, Spring MVC 라고 부른다.
Spring MVC는 클라이언트 요청을 아래 그림처럼 처리한다.

Controller 클래스는 Spring MVC에서 클라이언트 요청의 최종 목적지이다.
여기서 ‘controller, dto’ 패키지는 API 계층에 해당되고, ‘model, service’ 패키지는 비즈니스 계층에 해당되며, repository는 데이터 액세스 계층에 해당된다.
우선 Controller 클래스 코드를 작성하기 전에 클라이언트 요청엔 어떤게 있을까 고민해보는게 필요한데, 이부분은 예전 기획서 쓸 때를 생각하니까 어렵다!!!!! 로 느껴지진 않는다.
이런 기능들이 필요하고, Member(회원), Coffee(커피), Order(주문) 리소스로 나눠지므로 3개 리소스에 해당하는 Controller 클래스를 작성한다.
엔트리 포인트는 Spring Initializr으로 생성한 프로젝트에 자동으로 생성되어있다.
세개 리소스 다 비슷해서 memberController에 클라이언트 요청을 처리할 핸들러 메서드를 추가한 것만 올린다.
@RestController
@RequestMapping("/v1/members")
public class MemberController {
@PostMapping
public ResponseEntity postMember(@RequestParam("email") String email,
@RequestParam("name") String name,
@RequestParam("phone") String phone) {
// JSON 문자열 수작업을 Map 객체로 대체
Map<String, String> map = new HashMap<>();
map.put("email", email);
map.put("name", name);
map.put("phone", phone);
//리턴 값을 ResponseEntity 객체로 변경
return new ResponseEntity<>(map, HttpStatus.CREATED);
//Http응답 상태를 함께 전달하면 클라이언트의 요청을 서버가 어떻게 처리했는지 쉽게 알 수 있다.
}
@GetMapping("/{member-id}")
public ResponseEntity getMember(@PathVariable("member-id") long memberId) {
System.out.println("# memberId: " + memberId);
// not implementation
//리턴 값을 ResponseEntity 객체로 변경
return new ResponseEntity<>(HttpStatus.OK);
}
@GetMapping
public ResponseEntity getMembers() {
System.out.println("# get Members");
// not implementation
//리턴 값을 ResponseEntity 객체로 변경
return new ResponseEntity<>(HttpStatus.OK);
}
}
오늘 과제는 Controller 구현 실습이었는데, 엄청나게 힘들었다.. 정말 무슨말인지 하나도 모르겠고, 이전에 한건 다 어디로 갔는지 기억이 하나도 안났다..
https://developer.mozilla.org/ko/docs/Web/HTTP/Status
https://radiant515.tistory.com/260

그랬더니

이렇게 {}가 함께 나왔다..
왜그럴까...??@@@@!!!!

이렇게 return 값을 new ResponseEntity<>(members, HttpStatus.NO_CONTENT);
라고 해주니까
원하는대로 잘 나왔다.
