@RestController : 해당 클래스를 REST API로 처리한다.
@RequestMapping : URL을 지정해준다.
RequestMapping에서 메소드를 지정하여 사용할 수도 있지만 아래에 있는 다른 Annotation으로 간편하게 사용할 수 있다.
@RestController //해당클래스를 REST API로 처리
@RequestMapping("/api/get") //URL을 지정
public class GetApiController {
//http://localhost:8080/api/get/request-hello
@RequestMapping(method = RequestMethod.GET, path = "/request-hello")
public String requestHello() {
return "request Hello";
}
//http://localhost:8080/api/get/hello
@GetMapping(path = "/hello") //path를 사용하지 않아도 똑같음
public String getHello() {
return "get Hello";
}
}
@PathVariable : URL에 변수가 들어가야할 때 사용한다.
두가지 경우로 사용할 수 있는데 첫번째 경우는 일반적으로 변수의 이름을 맞출 수 있을때 사용하고 만약 변수의 이름을 맞출 수 없다면 두번째 경우처럼 사용한다.
@RestController
@RequestMapping("/api/get")
public class GetApiController {
//http://localhost:8080/api/get/path-variable1/{id}
@GetMapping("/path-variable1/{id}")
public String pathVariable1(@PathVariable String id) { //pathVariable의 변수이름과 맞출 수 있을 때
System.out.println("PathVariable = " + id);
return id;
}
//http://localhost:8080/api/get/path-variable2/{id}
@GetMapping("/path-variable2/{id}")
public String pathVariable2(@PathVariable(name = "id") String pathName) { //pathVariable의 변수이름을 맞추지 못할때 사용
System.out.println("PathVariable = " + pathName);
return pathName;
}
}
@RequestParam : query-parameter를 통해 어떠한 값을 받아올 수 있다.
크게 3가지 방법으로 사용할 수 있는데
1. map을 이용하여 어떠한 값이라도 받아오기
2. 명시적으로 타입을 지정하여 해당 타입만 받아오기
3. DTO같은 클래스를 사용하여 받아오기
@RestController
@RequestMapping("/api/get")
public class GetApiController {
//http://localhost:8080/api/get/query-param1?key1=value1&key2=value2
@GetMapping("/query-param1")
public String queryParam1(@RequestParam Map<String, String> queryParam) { //Map을 사용했기때문에 어떠한 값이라도 들어올 수 있다
StringBuilder sb = new StringBuilder();
queryParam.entrySet().forEach(entry -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println();
sb.append(entry.getKey() + " = " + entry.getValue() + "\n");
});
return sb.toString();
}
//http://localhost:8080/api/get/query-param2?name=이름&email=이메일&age=123
@GetMapping("/query-param2") //map을 사용하지않고 명시적으로 String과 int형으로 정해줬기 때문에 알맞은 값이 나오지 않으면 400
public String queryParam2(
@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;
}
//http://localhost:8080/api/get/query-param3?name=이름&email=이메일&age=123
@GetMapping("/query-param3") //2번의 방식으로 사용하면 변수가 많을때는 사용하기가 힘들기 때문에 미리 정의된 dto를 사용한다
public String queryParam3(UserRequest userRequest) { //@RequestParam은 없어야 정상적으로 작동한다
System.out.println(userRequest.getName());
System.out.println(userRequest.getEmail());
System.out.println(userRequest.getAge());
return userRequest.toString();
}
}
@RequestBody : body를 이용해 값들을 받아올 수 있다.
위의 경우처럼 여러방법으로 사용할 수 있다.
@RestController
@RequestMapping("/api")
public class PostApiController {
//http://localhost:8080/api/post1
@PostMapping("/post1") //body부분에 값들을 넣어줘야 함
public void post1(@RequestBody Map<String, Object> requestData) {
requestData.entrySet().forEach(stringObjectEntry -> {
System.out.println("key : " + stringObjectEntry.getKey());
System.out.println("value : " + stringObjectEntry.getValue());
});
}
//http://localhost:8080/api/post2
@PostMapping("/post2") //body부분에 값들을 넣어줘야 함
public void post2(@RequestBody PostRequestDTO postRequestDTO) { //@RequestBody를 사용하지 않으면 null값이 들어감 get때는 넣으면 에러인데 여기는 왜?
System.out.println(postRequestDTO.toString());
}
}
@JsonProperty : Json에서 값을 받아올 때 원래라면 케이스가 맞지않다면 null이 들어오는데 이 어노테이션을 사용하면 스네이크케이스를 카멜케이스처럼 받을 수 있다. 또한 특정한 값을 받는 것도 가능하다.
@JsonProperty("phone_number") //josn에서 다른이름으로 들어왔을때도 똑같이 처리시켜줌
private String phoneNumber; //phone_number