- 학습 목표
- Presentation layer를 이해할 수 있다.
- HTTP의 4가지 메소드를 이해할 수 있다.
- 싱글톤을 이해할 수 있다.
- Docker를 이해하고 활용할 수 있다.
- 실습 구성
- Presentation → Business → Persistence 레이어드 아키텍처 방식으로 Member CRUD 구현하기
- DB는 사용하지 않는다. (List 메모리로 활용)
- Docker image 만들고 도커 허브에 배포
- 도커 허브에서 이미지 다운로드하여 확인
- 과제
- 실습 못한 부분 완성
1차시
복습
- applicationContext에서 꺼내서 씀.
- singleton 개념과 매핑.
- Configuration은 singleton 방식으로 운용된다.
- 컨테이너 기반으로 묶음 단위로 운용한다.
step1
step1.5 웹 브라우저 HTTP protocol(URL이 어떤 절차와 형식을 통해 만들어지는지)
Dispatcher Servlet
step2 @Controller(presentation layer)
- Create
- Read
- Update
- Delete
step3 @Service(business layer)
step4 @Repository
DB(database layer)
실습
Settings를 singleton pattern으로 하나를 돌려 쓴다.
keyword Bean scope, Bean 생명주기
http method
PostMapping → Create
GetMapping → Read
PutMapping → Update
DeleteMapping → Delete
package com.example.day4.web;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/latest")
public class TestController {
// Create
@PostMapping("/test")
// <http://localhost:8080/api/latest/test>
public ResponseEntity<String> create() {
return null;
}
// Read - 단건 조회
@GetMapping("/test/{id}")
// <http://localhost:8080/api/latest/test1>
// <http://localhost:8080/api/latest/test2>
// <http://localhost:8080/api/latest/test3>
public ResponseEntity<String> read(Long id) {
return null;
}
// Update
@PutMapping("/test/{id}")
// <http://localhost:8080/api/latest/test1>
// <http://localhost:8080/api/latest/test2>
// <http://localhost:8080/api/latest/test3>
public ResponseEntity<String> update(Long id) {
return null;
}
// Delete
@DeleteMapping("/test/{id}")
// <http://localhost:8080/api/latest/test1>
// <http://localhost:8080/api/latest/test2>
// <http://localhost:8080/api/latest/test3>
public ResponseEntity<String> delete(Long id) {
return null;
}
}
Dispatcher Servlet은 집배원 역할이다.
위와 같이 동일 주소일 때 어떤 기능으로 가야할지 모른다.
http://HOST:port
port : 구분할 수 있는 번호
프로젝트 전에 aws, docker 실습해보기
Microsoft Windows [Version 10.0.22621.1848]
(c) Microsoft Corporation. All rights reserved.
C:\\Users\\ifiam>docker --version
Docker version 23.0.5, build bc4487a
C:\\Users\\ifiam>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\\Users\\ifiam>docker tag docker-spring:0.0.1 yerinpark0208/knu-spring:0.0.1
C:\\Users\\ifiam>docker run -p 8080:8080 yerinpark0208/knu-spring:0.0.1
Error: Invalid or corrupt jarfile /app.jar
C:\\Users\\ifiam>docker run -p 8080:8080 yerinpark0208/knu-spring:0.0.1
Error: Invalid or corrupt jarfile /app.jar
C:\\Users\\ifiam>docker tag docker-spring:0.0.1 yerinpark0208/knu-spring:0.0.1
C:\\Users\\ifiam>docker run -p 8080:8080 yerinpark0208/knu-spring:0.0.1
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.13)
C:\\Users\\ifiam>docker ps -a
C:\\Users\\ifiam>docker rm b61fc1998
C:\\Users\\ifiam>docker ps -a
C:\\Users\\ifiam>docker push yerinpark0208/knu-spring:0.0.1
denied: requested access to the resource is denied
C:\\Users\\ifiam>docker login
C:\\Users\\ifiam>docker image rm docker-spring:0.0.1
Untagged: docker-spring:0.0.1
C:\\Users\\ifiam>docker image rm 4b8c
C:\\Users\\ifiam>docker pull yerinpark0208/knu-spring:0.0.1
0.0.1: Pulling from yerinpark0208/knu-spring
C:\\Users\\ifiam>docker images
docker
Container base 배포 자동화(CI/CD)
GitHub을 이용하는 방법
GitHub Action
깃헙에 커밋, 푸쉬하는 순간 모든 배포가 자동으로 이루어진다. 사람이 했을 때 오류가 날 가능성을 줄여준다.
step4. AWS EC2
step5. image → run(컨테이너화)
cf. 볼륨이 큰 대기업에서는 컨테이너들끼리의 관계를 자동화해주고 관리해주는 시스템이 등장.
컨테이너를 관리(쿠버네티스) // 쿠버네티스는 도커와 다른 개념이다.
데브옵스(예전의 배포팀)가 이런 것들을 사내 인프라에 구성해놓는다.
aws 기술 면접 질문 : 네트워크, 웹에 대한 전반적인 이해를 묻는다.
쿠버네티스로 복잡한 인프라를 구성하기까지는 신입한테 어려움.
Roadmap
단계별 공부법(기간 설정하기)
- 스프링 부트 코딩 실력 높이기
- DB 자체의 실력 높이기
- 관계형 데이터베이스 책 한 권 사서 공부하기
- Docker, AWS 사용 경험
- Kubernetes
- 뭔지는 알고 있을지 몰라도 차라리 Redis처럼 session management를 해보기
Database의 schema setup
실습 코드
package com.example.day4.web;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/latest")
public class TestController {
/*
* postman이라는 프로그램으로 프로젝트를 실행하고
* 기능이 잘 동작하는지 테스트해본다.
**/
// Create
@PostMapping("/test")
// http://localhost:8080/api/latest/test
public ResponseEntity<String> create(@RequestBody Map<String, String> map) {
// val1
// val2
// intellij 단축키 soutv
System.out.println("map.val1 = " + map.get("val1"));
System.out.println("map.val2 = " + map.get("val2"));
return ResponseEntity.ok("CREATED");
}
// Read - 단건 조회
@GetMapping("/test/{id}")
// http://localhost:8080/api/latest/test1
// http://localhost:8080/api/latest/test2
// http://localhost:8080/api/latest/test3
public ResponseEntity<String> read(@PathVariable Long id) {
System.out.println("id = " + id);
return ResponseEntity.ok("Hello world KNU");
}
// Update
@PutMapping("/test/{id}")
// http://localhost:8080/api/latest/test1
// http://localhost:8080/api/latest/test2
// http://localhost:8080/api/latest/test3
public ResponseEntity<String> update(@PathVariable Long id, @RequestBody Map<String, String> map) {
System.out.println("id = " + id);
System.out.println("map.val1 = " + map.get("val1"));
System.out.println("map.val2 = " + map.get("val2"));
return ResponseEntity.ok("UDPATED");
}
// Delete
@DeleteMapping("/test/{id}")
// http://localhost:8080/api/latest/test1
// http://localhost:8080/api/latest/test2
// http://localhost:8080/api/latest/test3
public ResponseEntity<String> delete(@PathVariable Long id) {
System.out.println("id = " + id);
return ResponseEntity.ok("DELETED");
}
}
관련 개념
1. postmapping / getmapping / putmapping / deletemapping
기존에 사용하던 방식을
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
다음과 같이 쓸 수 있다.
@GetMapping("/get/{id}")
나머지도 마찬가지이다.
@GetMapping - shortcut for @RequestMapping(method = RequestMethod.GET)
@PostMapping - shortcut for @RequestMapping(method = RequestMethod.POST)
@PutMapping - shortcut for @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping - shortcut for @RequestMapping(method =RequestMethod.DELETE)
@PatchMapping - shortcut for @RequestMapping(method = RequestMethod.PATCH)
참고 링크
Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
In this article, we will discuss Spring 4.3. introduced HTTP method specific shortcut variants of @RequestMapping are @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping annotations with sample code examples.
www.javaguides.net
2. ResponseEntity
ResponseEntity는 헤더, 본문 및 상태를 포함한 HTTP 응답을 나타냅니다. @ResponseBody가 반환 값을 응답 본문에 넣는 동안 ResponseEntity를 사용하면 헤더와 상태 코드를 추가할 수도 있습니다.
'TIL' 카테고리의 다른 글
[코드스테이츠] Day07 BE (0) | 2023.07.05 |
---|---|
[코드스테이츠] Day06 BE (0) | 2023.07.03 |
[코드스테이츠] Day05 BE (0) | 2023.06.30 |
[코드스테이츠] Day03 BE (0) | 2023.06.29 |
[코드스테이츠] Day02 BE (0) | 2023.06.28 |