ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • content-Type 에 따른 컨트롤러 Request 어노테이션
    BackEnd/스프링 2021. 12. 17. 17:32
    Content-Type : application/json; charset=UTF-8 client 가 보내는 정보의 형식
    Accept: application/json
    *Quick note: there are other ways to do content negotiation, but the Accept header is the default way
    client 가 응답받고자 하는 정보의 형식

     

    http 요청 파라미터는 url-encoded 방식 > ../action.do?name=''&age=''

    http body 는 json 혹은 form 방식으로 들어온다.

     


    @RequestParam 

    클라이언트가 전송하는 파라미터를 1:1로 받기 위해 사용하고,  Http 요청 파라미터를 받는다,

    @RequestParam 을 쓰면 url encoded 방식으로 데이터를 받기 때문에, multipart 에 받으면 안들어옴
    content type 'multipart/form-data boundary= charset=utf-8' not supported

     

    Do I need to URL encode @RequestParams? > Yes, because Spring automatically URL decodes them
    marco+wantsnospam@marcobehler.com

    will be "marco[space]wantsnospam@marcobehler.com",

    as Spring will replace the + with a space, which is proper RFC3986 handling.

    https://www.marcobehler.com/guides/spring-mvc#difference-controller-restcontroller


    @RequestBody 

    스프링이 Content-Type 을 확인해  Http Request Body 로 전달된 JSON, XML, Text 등의 데이터가 적합한 HttpMessageConverter를 통해 파싱되어 Java 객체로 변환 된다.

     

    json 을 자바 객체로 변환할 때 MessageConverter 중 MappingJackson2HttpMessageConverter를 사용하고, 이는 ObjectMapper 가 사용된다.
    ObjectMapper 는 기본 생성자 혹은 setter 로 값을 찾기 때문에 둘 다 없으면 에러가 난다.
    @Getter 도 항상 있어야 한다.


    @ModelAttrubute

    클라이언트가 전송하는 여러 파라미터를 1:1로 객체에 바인딩 한 후 다시 View로 데이터를 넘겨서 출력하기 위해 사용한다.
    multipart/form-data 형태의 HTTP Body 나 

    HTTP  파라미터값들을 생성자나 setter 통해 주입한다.


    @ModelAttribute는 넘어온 파라미터의 타입이 객체의 타입과 일치하는지를 포함해서 다양한 유효성(Validation) 검사를 추가적으로 진행합니다.


     @RequestPart 

    enctype="multipart/form-data" 를 보낼 때 Request Body 에 encoding 되어 들어와서 request.getParameter 로는 null 이 반환된다. Part 에 접근할 때는 @MultipartConfig나 getParts() 를 통해 접근할 수 있다. 

    HTTP 요청 시 컨텐트 타입이 multipart/로 시작되는 경우를 멀티파트 요청으로 판단한다.
    멀티파트 필터가 동작하는 과정해서 멀티파트 리졸버에 의해 멀티파트 관련 함수를 제공하는 MultipartHttpServletRequest로 요청 클래스가 래핑된다.
    래핑된 요청 클래스는 RequestMappingHandlerAdapter에서 HandlerMethodArgumentResolver로 핸들러 매개변수에 대한 데이터 바인딩을 시도한다.


    @RequestParam는 @RequestPart보다 우선적으로 사용된다.
    @RequestParam 이 query parameters(query string) 만 받아준다.



    https://stackoverflow.com/questions/34881398/request-getparameter-is-always-null-when-using-enctype-multipart-form-data
    https://stackoverflow.com/questions/17937841/multipart-form-data-does-not-support-for-request-getparamerter


    The main difference is that when the method argument is not a String or raw MultipartFile / Part , 
    @RequestParam relies on type conversion via a registered Converter or PropertyEditor 
    while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part.

     

    https://galid1.tistory.com/754

    반응형

    'BackEnd > 스프링' 카테고리의 다른 글

    스프링 비동기  (0) 2021.12.31
    스프링 시큐리티  (0) 2021.12.29
    스으프르리잉  (0) 2021.10.22
    라이브러리를 만들어볼까  (0) 2021.10.22
    토비의 스프링 1장 Ioc  (0) 2021.10.12
Designed by Tistory.