PDH 개발 공부

MessageConverter 본문

WEB Programing/Spring

MessageConverter

IFBB 2021. 7. 3. 08:20

MessageConverter

메시지 컨버터란?

  • 우리는 HTTP 요청을 모델에 바인딩하고 클라이언트에 보낼 HTTP 응답을 만들기 위해 뷰를 사용했던 방식
    위한 방식을 사용.

  • XML , JSON , AJAX 와 같은 데이터 포맷형 언어를 처리

  • Spring 에서는 @ResponseBody
    @RequestMapping 을 처리 할 때 사용.

  • @ResponseBody를 입력할 경우 리턴 타입에 맞는
    메세지 컨버터를 선택한 뒤 리턴 값을 통째로 메세지로 변환한 뒤 리턴

@ResponseBody 특징

  • 참고로 GET 방식의 요청일 경우 HTTP 요청 본문이 없으므로 @RequestBody를 사용할 수 없다.
  • String 값이 아닌 객체를 반환 할 경우에 Error
  • Spring 컨버터 등록법

여러 메시지 컨버터를  등록 할 수 있다.  기본 등록된  메시지  컨버터들은 설정이 해제


<mvc:annotation-driven>
    <mvc:message-converters>
        <bean . . . />
        <bean . . . />
    </mvc:message-converters>
</mvc:annotation-driven>


StringHttpMessageConverter

<bean class="org.springframework.http.converter.StringHttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
             <value>text/html; charset=UTF-8</value>
        </list>
    </property>
</bean>

pom.xml

<!-- jackson -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.1</version>
</dependency>

org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
   <property name="supportedMediaTypes">
       <list>
            <value>application/json; charset=UTF-8</value>
        </list>
   </property>
</bean>

jQuery , JavaScript와의 관계

  • 브라우저가 접근 할 수 있도록 객체를 제공 해주고 그 객체의 식별자를 통해 접근

'WEB Programing > Spring' 카테고리의 다른 글

Junit  (0) 2021.07.02
Comments