카테고리 없음

[Spring] Handler Method(핸들러 메소드)

디벨로펄 2023. 5. 2.
반응형

Spring Web에서 사용자의 요청을 받아서 응답을 리턴하는 메소드

1. 매핑정보 : GetMapping, PostMapping ...

2. 요청 : parameter

3. 응답 : return 값

@RequestMapping

- name : 뷰 템플릿에서 식별할 때 쓰는 이름, 실무에서는 별로 안씀.
- value, path : URI
- method : HTTP method (GET, POST, ...), http method로 filtering
- params : 파라미터 검사, 파라미터로 filtering
- headers : 헤더 검사, 헤더로 filtering
- consumes : 헤더의 content-Type 검사, content-Type으로 filtering
- produces : 헤더의 Accept 검사, accept로 filtering

 

□ 핸들러 메소드가 받을 수 있는 요청들(문서에 정리되어 있다고 한다.)

. ServletRequest, ServletResponse, HttpSession  - jsp 프로그래밍

. WebRequest, NativeWebRequest

. @RequestParam, @PathVariable - 자주 씀

. @RequestBody, HttpEntity<B> - 자주 씀

. Principal(인증 정보), Locale, TimeZone

등등 굉장히 많음

 

□ 핸들러 메소드가 보낼 수 있는 응답

. ModelAndView

. String, View,

. @ResponseBody - API 생성할 떄 많이 사용

. @ModelAttribute, Map, Model

. HttpEntity<B>, ResponseEntity<B>(ResponseBody랑 동일한 동작을함.)

. HttpHeaders : body는 없고 헤더만 리턴.

. void

등등 많음.

 

 

RequestParam에 대하여

동일한 동작을 한다. parameter가 필수가 아니다.
    @GetMapping("/places")
    public String adminPlaces(@RequestParam(required = false) PlaceType placeType){
        return "admin/places";
    }
    
    @GetMapping("/places")
    public String adminPlaces(PlaceType placeType){
        return "admin/places";
    }
    
    // parameter가 필수이다.
    @GetMapping("/places")
    public String adminPlaces(@RequestParam(required = true) PlaceType placeType){
        return "admin/places";
    }
    
    @GetMapping("/places")
    public String adminPlaces(@RequestParam PlaceType placeType){
        return "admin/places";
    }

 

 

반응형

댓글