본문 바로가기
STUDY/ETC

REST API 디자인가이드?

by NOTEEE 2018. 1. 22.

REST API 디자인가이드를 알아보자.


예전에 API개발할 땐 REST URI를 최대한 명시적으로 적었었다.


(get..., insert..., update...., 등등)


근데 이게 잘못된 방식이라고...



REST API 규칙 

  1.  리소스명은 동사보다 명사로 사용
  2.  HTTP Method(GET, POST, PUT, DELETE)로 표현

규칙에 따르면

POST /sample/insert/1 -> 이렇게 작성하면 잘못된 표현입니다.
POST /sample/1 -> 맞는표현.

POST : POST를 통해 해당 URI를 요청하면 리소스를 생성합니다.
GET : GET을 통해 해당 리소스를 조회합니다.
PUT : PUT를 통해 해당 리소스를 수정합니다.
DELETE : DELETE를 통해 리소스를 삭제합니다.



아래는 예제로 만들어본 코드입니다.


@RequestMapping(value = "" , method=RequestMethod.GET)
public @ResponseBody SampleEntitiy list(HttpServletRequest request) {

}
 
@RequestMapping(value = "/{idx}" , method=RequestMethod.GET)
public @ResponseBody SampleEntitiy one(HttpServletRequest request, 
@PathVariable final int idx) {
 
}
 
@RequestMapping(value = "" , method=RequestMethod.POST)
public @ResponseBody SampleEntitiy insert(HttpServletRequest request) {
 
}
 
@RequestMapping(value = "/{idx}" , method=RequestMethod.PUT)
public @ResponseBody SampleEntitiy update(HttpServletRequest request, 
@PathVariable final int idx) {
 
}
 
@RequestMapping(value = "/{idx}" , method=RequestMethod.DELETE)
public @ResponseBody ResponseResult sampleDelete(HttpServletRequest request, 
@PathVariable final int idx) {
 
}
cs

'STUDY > ETC' 카테고리의 다른 글

맥 자바버전 변경  (0) 2018.03.02
맥 자바설치  (0) 2018.03.02
bxSlider  (0) 2018.01.22
TMap API  (0) 2018.01.22
sourcetree  (0) 2018.01.22