BackEnd/HTTP
[ HTTP / 네트워크 ] @RestController - API PUT / DELETE 방식을 사용하는 방법
jimin-log
2023. 2. 26. 10:55
PUT / DELETE 방식을 사용하는 방법
메서드 | 의미 | 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 . PUT 업데이트(갱신)
- Controller : PostApiController
@RequestBody , 수정되는 내용을 Body로 받아온다. @PathVariable 로 수정될 고유 아이디를 받을 수 있다.
package com.example.put;
import com.example.put.dto.PostRequestDto;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class putApiController {
@PutMapping("/put/{userId}")
public PostRequestDto put(@RequestBody PostRequestDto postRequestDto, @PathVariable(name = "userId") Long id){
System.out.println(id);
System.out.println(postRequestDto);
return postRequestDto;
}
}
- Class (객체) : PostRequestDto
@JsonProperty 앞서 설명했던 json에서 사용한 객체의 이름을 각각 선언해주는 방식 외에도
클래스전체에 선언에 안에 들어간 명칭을 모두 일정한 형태로 변경 시켜 확인 가능하게 해주는 기능이 있다.
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
받은 이름을 스네이크케이스의 클레스로변경 시켜준다.
package com.example.put.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
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;
@Override
public String toString() {
return "PostRequestDto{" +
"name='" + name + '\'' +
", age=" + age +
", carList=" + 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;
}
}
- Class (객체) : carDto
package com.example.put.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
public class CarDto {
private String carName;
@JsonProperty("car_number")
private int carNumber;
public String getCarName() {
return carName;
}
@Override
public String toString() {
return "CarDto{" +
"carName='" + carName + '\'' +
", carNumber=" + carNumber +
'}';
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarNumber() {
return carNumber;
}
public void setCarNumber(int carNumber) {
this.carNumber = carNumber;
}
}
-실행결과
2 . DELETE 삭제
http://localhost:8010/api/delete/{userId}?key=value
http://localhost:8010/api/delete/100?account=user100
@DeleteMapping 으로 리소스를 설정 해준다.
삭제기능 같은 경우, 보통 고유한 번호를 직접 받아서 사용하거나, 파라미터로 받아서 제거해준다.
번호를 받는 것은 @PathVariable을 통해서, 아이디 값은 @RequestParam 으로 받아 보았다.
- Controller : DeleteController
package com.example.delete.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class DeleteController {
@DeleteMapping("/delete/{userId}")
public void delete(@PathVariable String userId, @RequestParam String account){
System.out.println(userId);
System.out.println(account);
//delete 리소스 삭제, 제거할 값이 이미 없어도 동일하게 200 ok를 반환한다.
}
}
- 실행결과