반응형
PUT METHOD
HTTP PUT요청이 들어왔을 때 동작
CRUD | 멱등성 | 안정성 | PathVariable | QueryParameter | DataBody |
C/U | O | X | O | △ | O |
멱등성: 같은 요청에 대해 여러번 반복해도 결과가 달라지지 않는 성질
Query Parameter : 데이터 처리하기위해 넘겨주는 매개변수(URL 중 ? 이후 문자열), &로 변수를 구분
ex) /user?id=abcd123&pw=1234
※ Query Parameter가 △인 이유?
해당 방식으로 데이터 생성할 수 있으나 일부 웹 서버는 URI길이가 제한 때문에, Query Parameter가 부적절한 경우 有
Path Variable : 데이터를 처리하기 위해 URI경로에 변수를 넣어줌
ex) /user/123
PUT 방식 요청에 대한 예제
package com.example.hello.controller;
import com.example.hello.dto.PutRequestDto;
import org.springframework.web.bind.annotation.*;
@RestController //API컨트롤러
@RequestMapping("/api")
public class PutApiController {
@PutMapping("/put")
public PutRequestDto put(@RequestBody PutRequestDto putRequestDto){
System.out.println(putRequestDto);
return putRequestDto; //JSON으로 응답할 떄 @JsonProperty, @JsonNaming에 의한 네이밍으로 내려감
}
//path-variable 활용
@PutMapping("/put/{userId}")
public PutRequestDto putId(@RequestBody PutRequestDto putRequestDto, @PathVariable Long userId) {
System.out.println(userId);
return putRequestDto;
}
}
package com.example.hello.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import java.util.List;
public class PutRequestDto {
private String name;
private int age;
//@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
//class 전체 Json 스네이크타입으로 변경해줌
@JsonProperty("car_list")
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 "PutRequestDto{" +
"name='" + name + '\'' +
", age=" + age +
", carList=" + carList +
'}';
}
}
package com.example.hello.dto;
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 + '\'' +
'}';
}
}
1)
2)
반응형
'웹개발 > Spring & SpringBoot' 카테고리의 다른 글
[Spring]의존성 주입(Dependency Injection, DI) - 미완 (0) | 2022.11.05 |
---|---|
REST API - (4) DELETE METHOD 예제 (0) | 2022.10.24 |
REST API - (2) POST METHOD 예제 (0) | 2022.10.23 |
REST API - (1) GET METHOD 예제 (0) | 2022.10.23 |