API GET 방식을 사용하는 방법
메서드 | 의미 | CRUD | 멱등성 | 안정성 | Path Variable |
Query Parameter |
DataBody |
GET | 리소스 취득 | R (read) | o | o | o | o | x |
POST | 리소스 생성,추가 | C (create) | x | x | o | △ | o |
PUT | 리소스 갱신,생성 | C / U (update) | o | x | o | △ | o |
DELETE | 리소스 삭제 | D | o | x | o | o | - |
HEAD | 헤더데이터 취득 | - | o | o | - | - | - |
OPTIONS | 지원하는 메서드 취득 | - | o | - | - | - | - |
TARCE | 요청 메시지 반환 | - | o | - | - | - | - |
CONNECT | 프록시 동작의 터널 접속으로 변경 | - | x | - | - | - | - |
1. GET 방식 파라미터
@GetMapping( "/ URI " )
http://localhost:9090/api/get/hello
1) 기본
@GetMapping("hello")
2) 명시적 지정
@GetMapping(path = "hello")
3) 예전에 사용하던 방식
@RequestMapping ( path = "/URI", method = RequestMethod.GET )
http://localhost:9090/api/get/hi
리퀘스트 맵핑으로 모든 전달 받는 get,post ..등을 전달 받을 수 있다.
때문에 method로 형식을 지정해주어야했다.
@RequestMapping(path = "/hi",method = RequestMethod.GET)
public String hi(){
return "get hi";
}
2. Path Variable (변화하는 구간)
http://localhost:9090/api/get/path-variable/ { name }
@GetMapping("/path-variable/{URI}")
public String pathVariable(@PathVariable String name){
1) 명칭과 변수가 같은 이름일때
@GetMapping("/path-variable/{name}")
public String pathVariable(@PathVariable String name){
System.out.println("pathVariable : " + name);
return name;
}
2) 변수에는 이름을 다르게 설정해야할때. (name="명칭") 으로 표시할 이름을 설정해준다.
@GetMapping("/path-variable/{id}")
public String pathVariable(@PathVariable (name = "id") String PathName){
System.out.println("pathVariable : " + PathName);
return PathName;
}
3. 쿼리 파라미터 (Query Param)
http://localhost:9090/api/get/query-param?key=value&key1=value1&key2=value2
@GetMapping(path="query-param")
public String queryParam (@RequestParam Map<String,String> queryParam){
1) key , value 의 형태로 Map으로 받을 수 있다.
하지만 key를 어떤 것을 받을지 명확히 지정해 줄수 없다.
@GetMapping(path="query-param")
public String queryParam(@RequestParam Map<String,String> queryParam){
StringBuilder sb = new StringBuilder();
queryParam.entrySet().forEach( entry ->{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
sb.append(entry.getKey() + "= " + entry.getValue() + "\n");
});
return sb.toString();
}
@GetMapping(path="query-param2")
public String queryParam2(@RequestParam String name,
@RequestParam String email,
@RequestParam int age
){
System.out.println("name : " + name);
System.out.println("email : " + email);
System.out.println("age : " + age);
return name + " "+ email + " "+ age;
}
3) 리퀘스트 DTO 형태로 맵핑 (현업에서 가장 많이 사용)
객채를 만들어서 쿼리파라미터를 미리 정의
API 메서드에 파라미터 안에 어노테이션을 선언할 필요가 없다. (@ RequestParam )
package com.example.hello2.dto;
public class UserRequest {
private String name;
private String email;
private int age;
//get / set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserRequest{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
@GetMapping(path="query-param3")
public String queryParam3(UserRequest userRequest){
System.out.println("name : " + userRequest.getName());
System.out.println("email : " + userRequest.getEmail());
System.out.println("age : " + userRequest.getAge());
return userRequest.toString();
}
'BackEnd > HTTP' 카테고리의 다른 글
[ HTTP / 네트워크 ] HTTP / 웹서버 / 웹 어플리케이션 서버 (0) | 2023.03.20 |
---|---|
[ HTTP / 네트워크 ] @RestController - API PUT / DELETE 방식을 사용하는 방법 (1) | 2023.02.26 |
[ HTTP / 네트워크 ] @RestController - API POST 방식 사용하는 방법 (0) | 2023.02.26 |
[ HTTP / 네트워크 ] 웹 개발 개론 -(2)HTTP Protocol (0) | 2023.02.24 |
[ HTTP / 네트워크 ] 웹 개발 개론 -(1) 웹 개발이란? (0) | 2023.02.24 |