인터페이스 3일차 (Controller)
2023. 1. 5. 12:16ㆍ코딩배움일지/인터페이스 구현
Controller
- MVC 디자인 패턴의 Controller에 해당
- View 와 Model 의 접점
- Client의 Request에 따라 모델의 상태를 변경
- Request URL의 end point에 해당하는 메서드를 실행
- End point 에 대한 Routing을 담당
- Service의 결과를 Client에게 Response
Spring Boot Controller 작성
@RestController
- JSON 형태의 Response를 반환하기 위한 Controller 임을 명시
- REST API 개발을 위해 사용됨 (Response로 HTML을 반환하지 않음)
- @Controller + @ResponseBody의 형태
@GetMapping(path)
- REST API의 GET method를 이용한 Request 중 ‘path’에 해당하는 end-point에 대한 라우팅을 담당
package com.jingu.board.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MainController {
@GetMapping("/")
public String hello() {
return "Hello Spring Boot World!";
}
}
package com.jingu.board.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/*Response 로 HTML을 반환하는 Controller 가 아닌
Response Body에 직접 데이터를 담아서 응답하는 Controller*/
/* @RestController = @Controller + @ ResponseBody*/
@RestController
public class MainController {
// http방식://호스트:포트/~ (/~ : end-point)
// http://localhost:4040/~~~ 에 해당하는 것
// @GetMapping(end-point) : 해당 end-point 로 Get 방식의 Request 가 왔을 때 동작
@GetMapping("/")
public String hello() {
return "Hello Spring Boot World!";
}
@GetMapping("/hello")
public String getHello() {
return "This is get method, end-point '/hello'";
}
}
Postman 사용하기
@RequestMapping(pattern)
- Request URL에서 특정 패턴에 해당하는 Request를 담당하는 Controller 임을 명시
- ‘https://host:port/’ 이후 패턴
@RestController
// @RequestMapping(pattern) : http://localhost:4040/(end-point)/
// end-point의 패턴을 지정하여 해당 패턴의 end-point 일 때 해당 Controller를 실행
@RequestMapping("apis")
public class MainController {
// http방식://호스트:포트/~ (/~ : end-point)
// http://localhost:4040/~~~ 에 해당하는 것
// @GetMapping(end-point) : 해당 end-point 로 Get 방식의 Request 가 왔을 때 동작
@GetMapping("/")
public String hello() {
return "Hello Spring Boot World!";
}
@GetMapping("/hello")
public String getHello() {
return "This is get method, end-point '/hello'";
}
}
@PostMapping(path)
- REST API의 GET method를 이용한 Request 중 ‘path’에 해당하는 end-point에 대한 라우팅을 담당
@PostMapping("hello")
public String postHello() {
return "This is post method, end-point '/hello'";
}
@PutMapping(path)
- REST API의 Put method를 이용한 Request 중 ‘path’에 해당하는 end-point에 대한 라우팅을 담당
// @PutMapping(end-point) : 해당 end-point로 Put 방식의 Request 가 왔을 때 동작
@PutMapping("hello")
public String putHello() {
return "This is put method, end-point '/hello'";
}
@PatchMapping(path)
- REST API의 Patch method를 이용한 Request 중 ‘path’에 해당하는 end-point에 대한 라우팅을 담당
// @PatchMapping(end-point) : 해당 end-point로 Patch 방식의 Request 가 왔을 때 동작
@PatchMapping("hello")
public String patchHello() {
return "This is patch method, end-point '/hello'";
}
@DeleteMapping(path)
- REST API의 Delete method를 이용한 Request 중 ‘path’에 해당하는 end-point에 대한 라우팅을 담당
// @DeleteMapping(end-point) : 해당 end-point로 Delete 방식의 Request 가 왔을 때 동작
@DeleteMapping("hello")
public String deleteHello() {
return "This is delete method, end-point '/hello'";
}
@RequestParam(key)
- https://host:port/path?key=value&.. 형태로 데이터를 받아오기 위한 어노테이션
- default로 필수 값으로 인식
- 필수 값으로 인식하지 않기 위해서는 required=false 옵션 필요
- default 값을 지정하기 위해선 defaultValue=value 옵션 필요
- 여러 개의 데이터를 받을 때는 HashMap<String, String> 타입으로 받을수 있음 (이 때는 @RequestParam HashMap<String, String> 매개변수명 사용)
@GetMapping(HELLO)
// @RequsetParam(name="", value="", required = true, defaultValue="")
// : URL로 데이터를 받는 경우 (Get, Delete) 쿼리 형태로 데이터를 받음
// http://호스트:포트/end-point?name=value&...
public String getHello(@RequestParam("name")String name) {
return "This is get method, end-point '/hello' " + name;
}
@GetMapping(HELLO)
// @RequsetParam(name="", value="", required = true, defaultValue="")
// : URL로 데이터를 받는 경우 (Get, Delete) 쿼리 형태로 데이터를 받음
// http://호스트:포트/end-point?name=value&...
public String getHello(@RequestParam(name="name", required=false, defaultValue="james") String name) {
return "This is get method, end-point '/hello' " + name;
}
@PathVariable(path)
- https://host:port/path/{:data} 형태로 데이터를 받아오기 위한 어노테이션
- path의 일종이기 때문에 필수로 입력
@GetMapping(HELLO + "/{name}")
// @PathVariable(path) : URL 데이터를 받는 경우 (Get, Delete) path 형태로 데이터를 받음
// http:/호스트:포트/end-point/VARIABLE
public String getHelloName(@PathVariable("name")String name) {
return "This is get method, end-point '/hello' " + name;
}
@GetMapping(HELLO + "/{name}/spring") /*중괄호를 받아온다.*/
// @PathVariable(path) : URL 데이터를 받는 경우 (Get, Delete) path 형태로 데이터를 받음
// http:/호스트:포트/end-point/VARIABLE
public String getHelloName(@PathVariable("name")String name) {
return "This is get method, end-point '/hello' " + name;
}
'코딩배움일지 > 인터페이스 구현' 카테고리의 다른 글
인터페이스 4일차 (postman) (0) | 2023.01.06 |
---|---|
인터페이스 3일차 (DTO) (0) | 2023.01.05 |
인터페이스 2일차 ( Spring Framework & Dispatcher Servlet) (0) | 2023.01.04 |
인터페이스 2일차 (DI & IoC) (0) | 2023.01.04 |
인터페이스 구현 2일차 (Spring Framework, SOLID) (0) | 2023.01.04 |