Model: controller, Service, Mapper/Repository 와의 상호 작용
RestController : Service와 상호작용
Service: Mapper, Reopstiory와 상호작용
Mapper/Reopstiory: SqlSesion 및 DB와 상호작용
*ResponseEntity는 HttpStatusCode,Headers,Body로 구성되어 Json 형식으로 반환함.
구성요소 |
설명 |
@RestController |
@Controller 어노테이션과 @ResponseBody 어노테이션을 합쳐놓은 어노테이션. 클래스 상단에 @RestController 어노테이션을 선언하면 Method마다 @ResponseBody를 붙여 주지 않아도 됨. |
@PathVariable |
URL에 변수값을 전달받음 |
@RequestParam |
URL에 파라미터 값을 전달받음 |
@RequestBody |
@ResponseBody와는 정반대로 리턴이 HTML Body가 아닌 전달받은 값이 HTML Body |
* 알고 있는 어노테이션은 죄다 적지 않았습니다.. 지송해욤
ResponseEnity<Type>
메서드 | 내용 |
ok() | Http status code 값을 200으로 변경 |
header(content Type 등등) | Http Header Type 값을 메소드 매개변수 값으로 변경 |
body(Type) | Type을 HTML Body 값으로 변경 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | @RequestController("animalController") @RequestMapping("animal") publicc class AnimalController { @Autowired private AnimalService animalService; //동물 전체 조회 @GetMapping("/animals") // get방식 접근일시 public ResponseEntity<List<Animal>> getAnimals(){ List<Animal> animals=animalService.getAnimals(); return ResponseEntity.ok().body(animals); } //동물 신규 등록 @PostMapping("/animal") // post방식 접근일시 public ResponseEntity<String> createAnimal(@RequestBody Animal animal){ return ResponseEntity.ok().body(animalService.createAnimal(animal)); } //동물 선택 수정 @PutMapping("/animal") // put 방식 접근일시 public ResponseEntity<Intger> updateAnimal(@RequestBody Animal animal){ //단 requestbody의 값은 json 형식이므로 animal을 파싱해주는 메서드가 필요함. Integer count=fuelService.updateAnimal(animal); //업데이트 된 컬럼 수 count 저장 return ResponseEntity.ok().body(count); } } | cs |
본 글은 다소 주관적인 생각들을 포함하고 있어, 잘못된 정보를 제공할 수도 있습니다. 부족한 점이 있으면 댓글 남겨주세요. 추가 및 수정 하도록 하겠습니다. 감사합니다.
[SI에서 살아남기] 내맘대로 Mapper,Repostiory 역할 및 간단한 예시 (1) | 2020.05.08 |
---|---|
[SI에서 살아남기] 내맘대로 Service 역할 및 간단한 예시 (0) | 2020.05.07 |
[SI에서 살아남기] 내맘대로 Model의 역할 및 간단한 예시 (0) | 2020.05.07 |
댓글 영역