[스프링 부트 포토그램] 8강~ 10강 Controller (동작 방식, http, 쿼리스트링, 주소 변수 매핑)
IDE: IntetlliJ Ultimate
Spring Boot: 3.3.0
JDK: 17
[스프링 부트 포토그램] 1강 ~7강 환경설정 완료
강의의 앞부분은 환경설정과 관련된 부분이다.이미 설치되어 있거나, 아는 내용들은 간단하게 정리만 하려고 한다. 1강 환경설정 - Git 설치Git 설치는 Git 홈페이지에서 할 수 있음 (깃 홈페이
devje.tistory.com
8강 스프링부트 Controller - 기본 동작방식 이해하기
> 컨트롤러란? (FrontController와 Dispatcher)
- 요청을 할 떄마다 Java 파일이 호출됨
- 요청의 종류가 3개이면 3개의 Java 파일이 필요함
- 하나의 Java 파일에서 모든 요청을 받는 FrontController 사용
- 너무 많은 요청이 한곳으로 모이는 것을 방지하기 위해 도메인 별로 분기
- 분기의 일은 Dispatcher가 해줌 (ServletDispatcher, RequestDispatcher)
User 테이블 | 로그인 회원가입 |
UserController.java |
Board 테이블 | 글쓰기 글삭제 글수정 |
BoardController.java |
Product 테이블 | 상품등록 상품목록 |
ProductController.java |
스프링부트에서는 Dispatcher 기능을 제공함
우리는 Controller만 만들고, 잘 다룰 수 있으면 됨
9강 스프링부트 Controller - http 4가지 요청 방식
- 클라이언트가 웹서버에 요청
- 웹서버는 DB에 SELECT, INSERT, UPDATE, DELETE 요청을 해서 응답!
- GET(동사): 데이터 요청
- POST(동사): 데이터 전송 (http body 필요)
- PUT(동사): 데이터 갱신 (http body 필요)
- DELETE(동사): 데이터 삭제
💾 HttpController.java
package org.example.photogram_re.web;
import org.springframework.web.bind.annotation.*;
@RestController
public class HttpController {
@GetMapping("/get")
public String get() {
return "get 요청 완료";
}
@PostMapping("/post")
public String post() {
return "post 요청 완료";
}
@PutMapping("/put")
public String put() {
return "put 요청 완료";
}
@DeleteMapping("/delete")
public String delete() {
return "delete 요청 완료";
}
}
- `@Controller`: File을 응답하는 controller (client가 브라우저이면 .html 파일을)
- `@RestController`: Data를 응답하는 controller (client가 핸드폰이면 data)
`get`은 브라우저에서 요청되나, `post`, `put`, `delete`는 요청할 수 없기 때문에 [postman]에서 test할 수 있음
10강 스프링부트 Controller - 쿼리스트링, 주소변수매핑
> http 쿼리 스트링(querystring), 주소 변수 매핑(path variable)
- 구체적인 데이터 요청 시에 쿼리스트링이나 주소변수매핑이 필요함
- 스프링부트에서는 주소변수매핑을 주로 사용함
💾 QueryPathController.java
package org.example.photogram_re.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class QueryPathController {
@GetMapping("/chicken")
public String chickenQuery(@RequestParam("type") String type) {
return type + " 배달갑니다. (쿼리스트링)";
}
@GetMapping("/chicken/{type}")
public String chickenPath(@PathVariable("type") String type) {
return type + " 배달갑니다. (주소 변수 매핑)";
}
}
public String chickenQuery(String type) {
public String chickenQuery(@RequestParam("type") String type) {
- `String type` 자동 바인딩 불가로, `RequestParam("Type")` 추가
public String chickenPath(@PathVariable String type) {
public String chickenPath(@PathVariable("type") String type) {
- `@PathVariable String type` 바인딩 불가로 `@PathVariable("type")` 처럼 name을 명시적 매핑을 함
[ERROR] Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection.
2024-06-03T20:44:01.912+09:00 ERROR 12604 --- [test] [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentEx
devje.tistory.com
[ERROR] Name for argument of type [int] not specified, and parameter name information not available via reflection
기존 @GetMapping("/user/{id}") public String profile(@PathVariable int id) { return "user/profile"; }id가 인식이 안 됨 java.lang.IllegalArgumentException: Name for argument of type [int] not specified, and parameter name information not availabl
devje.tistory.com
이지업클래스 [메타코딩] 스프링부트 SNS프로젝트 - 포토그램 만들기 강의 실습