5. PUT API
PUT 은 첫 한번 데이터가 생성되고 그 다음에는 계속 데이터가 업데이트 되기 때문에 데이터는 항상 하나이므로 멱등하다고 할 수 있다.
잘못된 데이터도 전송하기 때문에 안정성은 낮고 Query Parameter는 따로 설계하지 않는 것이 권장된다.
import com.example.put.dto.PostRequestDto;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class PutApiController {
@PutMapping("/put")
public void put(@RequestBody PostRequestDto requestDto){
System.out.println(requestDto);
}
}
컨트롤러
public class CarDto {
private String name;
private String carNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
@Override
public String toString() {
return "CarDto{" +
"name='" + name + '\'' +
", carNumber='" + carNumber + '\'' +
'}';
}
}
public class PostRequestDto {
private String name;
private int age;
private List<CarDto> carList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<CarDto> getCarList() {
return carList;
}
public void setCarList(List<CarDto> carList) {
this.carList = carList;
}
@Override
public String toString() {
return "PostRequestDto{" +
"name='" + name + '\'' +
", age=" + age +
", carList=" + carList +
'}';
}
}
이유: car_list는 스네이크 케이스 코드에 정의는 carList 카멜 케이스로 되어있다.
클래스에 대해서 전체적으로 같은 룰을 전략에 맞게 바꾸는 방법이 있다.
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PostRequestDto {
..
후략
..
'com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy' is deprecated
*SnakeCaseStrategy는 deprecated 되었다고 한다.
PostRequestDto{name='steve', age=20, carList=[CarDto{name='audi', carNumber='null'}, CarDto{name='A4', carNumber='null'}]}
이번엔 carNumber가 들어오지 않았다.
@JsonProperty("car_number")
public String getCarNumber() {
return carNumber;
}
@JsonNaming으로 전부 처리하도록 선언할 수 있고 @JsonProperty처럼 특정 변수만 처리할 수 있다.
보내고자 했던 데이터를 어떻게 response를 내려줄까?
RestController 같은 경우에는 Object 자체를 리턴시키면 스프링부트 자체에서 이 오브젝트를 매퍼를 통해서 Json으로 변환해 준다.
@RestController
@RequestMapping("/api")
public class PutApiController {
@PutMapping("/put")
public PostRequestDto put(@RequestBody PostRequestDto requestDto){
System.out.println(requestDto);
return requestDto;
}
}
그대로 리스펀드가 온다.
스프링부트 RestController에서는 Object로 리턴을 시키고, Jsonproperty나 JsonNaming으로 전략을 설정해두면 거기에 따라서 Json으로 변환되어 Response로 내려간다.
객체 형태에 룰을 적용하면 그대로 Json으로 내려가 개발이 가능하다.
정리: 어떠한 변수로 만들고 데이터를 저장할 것인지 클래스를 디자인 하는것이 우선이다.
@PutMapping("/put/{userId}")
public PostRequestDto put(@RequestBody PostRequestDto requestDto, @PathVariable Long userId){
System.out.println(userId);
return requestDto;
}
}
@PathVariable 에 대해서는 {변수이름}과 메서드에 선언하는 변수이름은 같아야 하며 안된다면 (name = "변수이름) 으로 맞춰 주어야 한다.