2025-04-07
2025-04-08
💻 Spring Boot로 서버 만들기 (Youtube)
💻 Spring Boot로 서버 만들기 (Inflearn)
@SpringBootApplication : Spring에 있는 다양한 설정들을 모두 자동으로 해주는 키워드class LibraryAppAPplication : 클래스 명SprinAppApplication.run(LibraryAppApplication.class, args); : LibraryAppApplication을 실행한다.System.out.println() : 출력문⇒ 컴퓨터에게 원하는 정보를 얻으려면 '요청'을 해야 한다. 요청은 '인터넷', '네트워크'를 통해 할 수 있다.
IP = Domain Name
⇒ 이러한 체계를 Domain name System(DNS)라고 한다.
운송장 표준 1
- 운송장을 받는 사람에게 요청하는 행위
- 운송장이 가는 집 (주소, IP)
- 운송장을 실제로 받는 사람 (PORT)
- 운송장을 받는 사람에게 원하는 자원 (어떤 항목을 원하는가)
- 자원의 세부 조건
운송장 표준 2
- 운송장을 받는 사람에게 요청하는 행위
- 운송장을 받는 사람
- 운송장을 받는 사람에게 원하는 자원
- 실제 자원이 들어 있는 박스
HTTP 예시 1
GET /portion?color=red&count=2 HOST: spring.com:3000
GET : HTTP 요청을 받는 컴퓨터에게 요청하는 행위 (데이터를 달라) (HTTP Method)Host: spring.com:3000 (IP:PORT) : HTTP 요청을 받는 대상 (컴퓨터와 프로그램 정보)/portion : HTTP 요청 받는 컴퓨터에게 원하는 자원 (path라고 함)? : 구분 기호 | color=red : 자원의 세부 조건 | & : 구분 기호 | count=2 : 자원의 세부 조건HTTP 예시 2
POST /oak/leather Host: spring.com:3000 오크가죽정보
POST : HTTP 요청을 받는 컴퓨터에게 요청하는 행위 (저장하라) (HTTP Method)Host: spring.com:3000 : HTTP 요청을 받는 대상 (컴퓨터와 프로그램 정보)/oak/leather : HTTP 요청을 받는 컴퓨터에게 원하는 자원오크가죽정보 : 실제 저장할 오크 가죽 정보GET /portion?color=red&count=2
HOST: spring.com:3000
POST /oak/leather
Host: spring.com:3000
오크가죽정보
POST /oak/leather <method query>
Host: spring.com:3000 <header>
<->
오크가죽정보 <body>
http://spring.com:3000/portion?color=red&count=2http : 사용하고 있는 프로토콜 (HTTP):// : 구분 기호spring.com:3000 : 도메인이름:포트, 도메인 이름은 IP로 대체 가능/ : 구분 기호portion : 자원 경로 (path)? : 구분 기호color=red&count=2 : 추가 정보200 OK | 300 Moved Permanently | 404 NotFound | 500 Internal Server ErrorHTTP/1.1 200 OK
Content-Type: appication/json
{
"name" : "A",
"age" : null
}
- (웹을 통한) 컴퓨터 간의 통신은 HTTP 라는 표준화도니 방식ㅇ ㅣ있다.
- HTTP 요청은 HTTP Method (GET, POST)와 Path(
/portion/)가 핵심이다.- 요청에서 데이터를 전달하기 위한 2가지 방법은 쿼리와 바디이다.
- HTTP 응답은 상태 코드가 핵심이다.
- 클라이언트와 서버는 HTTP를 주고 받으며 기능을 동작하는 이때 정해진 규칙을 API라고 한다.
GET /portion?color=red&count=2
Host: spring.com:3000
@RestController
public class CalcualtorController {
@GetMapping("/add"); // GET /add
public int addTwoNumbers(@RequestParam int number1, @RequestParam int number2)
{
return number1 + number2;
}
}
@RestController : 주어진 Calss를 Controller로 등록한다.@GetMapping("/add") : 아래 함수를 HTTP Method가 GET이고 HTTP Path가 /add인 API로 지정한다.@RequestParam : 주어지는 쿼리를 함수 파라미터에 넣는다.⇒ GET /add?number1=10&number2=20
{
"name" : "정수현",
"age" : 23
}
[CaculatorController.java]
@PostMapping("/multiply")
public int multiplyTwoNumbers(@RequestBody CalculatorMultiplyRequest request)
{
return request.getNumber1() * request.getNumber2();
}
@RequestBody : HTTP Body로 들어오는 JSON을 CalculatorMultiplyRequest로 바꾸어준다CalculatorMulltiplyRequest : HTTP Body를 객체로 바꾸는 @RequestBody를 사용하는 경우, 생성자를 만들지 않아도 괜찮다.[CalculateMultiplyRequest]
public class CalculatorMultiplyRequest {
private final int number1;
private final int number2;
public CalculatorMultiplyRequest(int number1, int number2)
{
this.number1 = number1;
this.number2 = number2;
}
public int getNumber1()
{
return number1;
}
public int getNumber2()
{
return number2;
}
}
CalculatorMultiplyRequest에 매핑된다