반응형
PUT API
리소스 갱신 및 생성
CRUD : C / U
멱등성 : O
안정성 : X
Path Variable : O
Query Parameter : 가능하나 권장하지 않음
Data Body : O
PutApiController.java
import com.example.demo.putAPI.PostRequestDto;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class PutApiController {
@PutMapping("/put")
public PostRequestDto put(@RequestBody PostRequestDto requestDto) {
System.out.println(requestDto);
return requestDto;
}
@PutMapping("/put2/{userId}")
public PostRequestDto put(@RequestBody PostRequestDto requestDto, @PathVariable(name = "userId") Long id) {
System.out.println(id);
return requestDto;
}
}
PostRequestDto.java
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.util.List;
//키값 이름을 카멜케이스와 스네이크 케이스 연동
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
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 +
'}';
}
}
CarDto.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class CarDto {
private String name;
@JsonProperty("car_number")
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 + '\'' +
'}';
}
}
반응형
'Spring' 카테고리의 다른 글
[Spring] Response(응답) 내려주기 (0) | 2022.08.09 |
---|---|
[Spring] DELETE API (0) | 2022.08.08 |
[Spring] POST API (0) | 2022.08.06 |
[Spring] GET API (0) | 2022.08.05 |
[Spring] Talend API설치 및 Hello Spring Boot 출력해보기 (0) | 2022.08.05 |
댓글