[Spring] REST API ex

ieed0205·2021년 4월 2일
0

🍃Spring

목록 보기
19/26
post-thumbnail

본 피드는 가공을 해야 사용할 수 있습니다.
↗참조링크

REST API


HTTP URI(Uniform Resource Identifier)를 통해 자원(Resource)을 명시하고, HTTP Method(POST, GET, PUT, DELETE)를 통해 해당 자원에 대한 CRUD Operation을 적용하는 것을 의미한다.

CRUD Operation
Create : 생성(POST)
Read : 조회(GET)
Update : 수정(PUT)
Delete : 삭제(DELETE)
HEAD: header 정보 조회(HEAD)

Ex

// Create
@RequestMapping(value = "/api/url", method = RequestMethod.POST)
public VO insertData(@RequestBody VO vo) throws Exception {	
	try{
		return service.insertData(vo);
	} catch(Exception e){
		throw e.getMessage();
	}
}

// Read
@RequestMapping(value = "/api/url/{dataId}", method = RequestMethod.GET)
public VO getData(@PathVariable(name="dataId") Integer dataId) throws Exception {	
	try{
		return service.getData(dataId);
	} catch(Exception e){
		throw e.getMessage();
	}
}

// Update
@RequestMapping(value = "/api/url/{dataId}", method = RequestMethod.PUT)
public VO updateData(@PathVariable(name="dataId") Integer dataId, @RequestBody VO vo) throws Exception {	
	try{
		VO vo_a = service.getData(dataId);
		if(  .isEmpty(vo_a) ) // 비어있을 때의 로직
			// 에러출력 "Not found data"
		return service.updateData(vo)
	} catch(Exception e){
		throw e.getMessage();
	}
}

// Delete
@RequestMapping(value = "/api/url/{dataId}", method = RequestMethod.DELETE)
public VO deleteData(@PathVariable(name="dataId") Integer dataId) throws Exception {	
	try{
		VO vo_a = service.getData(dataId);
		if(  .isEmpty(vo_a) ) // 비어있을 때의 로직
			// 에러출력 "Not found data"
		return service.deleteData(dataId);
	} catch(Exception e){
		throw e.getMessage();
	}
}

// List
@RequestMapping(value = "/api/url", method = RequestMethod.GET)
public VO getDataList(@RequestParam HashMap<String, String> params) throws Exception {	
	try{
		List<VO> dataList = service.getDataList(params);
		int total = service.getDataListCount(params);
	
		PageableList<VO> pageableList = getPageableList(dataList);
		pageableList.setTotal(total);
		pageableList.setCurrentPage(params.getCurrentPage());
		pageableList.setNumberOfRows(params.getRowSize());
			
		return pageableList;
	} catch(Exception e){
		throw e.getMessage();
	}
}
profile
🔥 머릿속으로 생각하지만 말고, 행동으로 보여줘

0개의 댓글