JeongJin's Blog

05. API를 작성하는 다양한 방법 (4) 본문

Book Study/스프링 부트 핵심 가이드

05. API를 작성하는 다양한 방법 (4)

정진킴 2023. 10. 23. 09:00

5.5 DELETE API 만들기

  • 데이터베이스 등의 저장소에 있는 리소스를 삭제할 때 사용하는 API 이다.
  • GET 메서드를 작성하는 방식과 동일하다.

5.5.1 @PathVariable 과 @RequestParam을 활용한 DELETE API 메서드 구현

  • pathVariable 소스
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/delete-api")
public class DeleteController {
	@DeleteMapping("/member/{variable}")
	public String member(
		@PathVariable("variable") String var
	) {
		return "Success Delete PathVariable : " + var;
	}
 }
  • 결과

  • requestParam 소스
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/delete-api")
public class DeleteController {
	@DeleteMapping("/member")
	public String member2(
		@RequestParam String variable
	) {
		return "Success Delete RequestParam : " + variable;
	}
}
  • 결과