내일배움캠프/TIL

[Spring_4기 본캠프] Spring 심화 - HttpMessageConverter | Day 52

austindynasty 2024. 12. 30. 20:13
  1. HttpMessageConverter: Spring MVC 에서 요청과 응답의 데이터 형식을 변환하는 인터페이스
    • Json Data를 Http Message Body에서 직접 읽거나 쓸 수 있다.
    • HTTP 요청 본문을 객체로 변경하거나, 객체를 HTTP 응답 본문으로 변경할 수 있다.
    • ByteArrayHttpMessageConverter
      • byte 배열 data를 처리
      • MediaType : * / * (전체)
      • 반환 : octet-stream
    • StringHttpMessageConverter
      • String data를 처리
      • MediaType : * / *
      • 반환 : text/plain
    • MappingJackson2HttpMessageConverter
      • Json Data를 처리한다.
      • 대상 : Object를 가장 많이 사용, Hashmap
      • MediaType : application/json
      • 반환 : application/json
    	// 요청 헤더 content-type: application/json
    @PostMapping("/json")
    // Data -> produces = "application/json"
    public Data json(@RequestBody Data data) {
    		log.info("json logic");
    		return data;
    }