@RestController
RestApi 기능을 가능하게 한다.
@RequestMapping("클래스 uri")
@RestController
@RequestMapping("/api/get")
public class GetApiController {
}
"서버의 도메인 + 해당 uri"로 접속할 시 해당 클래스 내부 중 하나의 메소드가 실행된다.
@GetMapping("메소드를 실행할 uri")
@GetMapping(path = "/hello") //http://localhost:8080/api/get/hello
public String hello(){
return "get hello";
}
"서버의 도메인 + 클래스 uri + 메소드를 실행할 uri"를 접속하면 해당 메소드에서 리턴하는 정보를 클라이언트가 Get한다.
@RequestMapping(method = Method.GET)
@RequestMapping(path = "/hi", method = RequestMethod.GET) //get http://localhost:8080/api/get/hi
public String hi(){
return "hi";
}
@GetMapping과 같은 기능을 수행한다.
@GetMapping(@pathVariable)
@GetMapping("/path-variable/{name}")
public String pathVariable(@PathVariable(name = "name") String pathname, String name){
System.out.println("PathVariable : " + pathname);
return pathname;
}
@GetMapping 내부의 "{}" 사이에 들어가는 이름과 @PathVariable 내부의 name = ""의 값과 같은 변수를 return 하여 클라이언트는 Get한다.
@GetMapping(path)
@GetMapping(@RequestParam)
//http:localhost:8080/query?name=x&email=y&age=10
@GetMapping(path = "query-param02")
public String queryParam02(
@RequestParam String name,
@RequestParam String email,
@RequestParam int age
){
System.out.println(name);
System.out.println(email);
System.out.println(age);
return name + " " + email + " " + age;
}
주석처리된 query를 처리한다.
http:localhost:8080/query?name=x&email=y&age=10에서 ?이후의 부분에 대해서
name=x
email=y
age=10 의 query를 @RequestParam이 처리한다.
DTO(Data Transfer Object)
DTO는 계층 간(Controller, View, Business Layer) 데이터 교환을 위한 자바 빈즈(Java Beans)를 의미한다.
DTO는 로직을 가지지 않는 데이터 객체이고 getter/setter 메소드만 가진 클래스를 의미한다.
* 많은 수의 query를 처리하기 어려우므로 처리해야할 query가 많은 경우 DTO 객체를 만들어 하나하나씩 @RequestParam 처리하는 것을 대신함.
@GetMapping("query-param03")
public String queryParam02(UserRequest userRequest){
System.out.println(userRequest.getName());
System.out.println(userRequest.getEmail());
System.out.println(userRequest.getAge());
return userRequest.toString();
}
'개발 인생 > 자바' 카테고리의 다른 글
Consider defining a bean of type of type ‘org.springframework.security.core.userdetails.UserDetailsService’ in your configuration. (0) | 2022.07.11 |
---|---|
[Spring] ApplicationContextAware (0) | 2022.03.15 |
[Spring] IoC/DI 제어의 역전/의존성 주입 (0) | 2022.03.12 |
[java] 람다식 (0) | 2022.02.07 |