RestController 작성에 사용되는 특별한 annotation은 다음과 같다.
@RestController
@RequestMapping("/boards")
public class BoardController {
@Inject
private BoardService service;
/**** CREATE ****/
@RequestMapping(value="", method=RequestMethod.POST)
public ResponseEntity<String> register(@RequestBody BoardVO vo) {
ResponseEntity<String> entity = null;
try {
service.addBoard(vo);
entity = new ResponseEntity<String>("SUCCESS", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return entity;
}
/**** READ ****/
@RequestMapping(value="/all", method=RequestMethod.GET)
public ResponseEntityList<List<BoardVO>> list() {
ResponseEntity<List<BoardVO>> entity = null;
try {
entity = new ResponseEntity<>(
service.listBoard(), HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<>(HttpStatus.BAT_REQUEST);
}
return entity;
}
/**** UPDATE ****/
@RequestMapping(value="/{bno}", method={RequestMethod.PUT, RequestMethod.PATCH})
public ResponseEntity<String> update(
@PathVariable("bno") Integer bno,
@RequestBody BoardVO vo) {
ResponseEntity<String> entity = null;
try {
vo.setBno(bno);
service.modifyBoard(vo);
entity = new ResponseEntity<String>("SUCCESS", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return entity;
}
/**** DELETE ****/
@RequestMapping(value="/{bno}", method=ReqeustMethod.DELETE)
public ResponseEntity<String> remove(
@PathVariable("bno") Integer bno) {
ResponseEntity<String> entity = null;
try {
service.removeBoard(bno);
entity = new ResponseEntity<String>("SUCCESS", HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
entity = new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
return entity;
}
}
출처: 코드로 배우는 스프링 웹 프로젝트 - 구멍가게 코딩단