본문 바로가기

Spring/Spring Boot

(SpringBoot) Spring Mvc(3) - ViewResolver

 

https://docs.spring.io/spring/docs/5.0.7.RELEASE/spring-framework-reference/web.html#mvc-config-message-converters

 

Web on Servlet Stack

This part of the reference documentation covers support for Servlet stack, WebSocket messaging that includes raw WebSocket interactions, WebSocket emulation via SockJS, and pub-sub messaging via STOMP as a sub-protocol over WebSocket. 4.1. Introduction The

docs.spring.io

Content Negotiation View Resolver 설정

들어오는 요청의 accept-header에 따라 응답이 달라진다. (View-Resolver 중의 일부) 

accept-header : 클라이언트 어떠한 타입의 본문을 원한다는 것을 서버에게 알려주는 것

 


Content Negotiation
WebFlux
ContentNegotiatingViewResolver does not resolve views itself but rather delegates to other view resolvers and selects the view that resembles the representation requested by the client. The representation can be determined from the Accept header or from a query parameter (for example, "/path?format=pdf").
The ContentNegotiatingViewResolver selects an appropriate View to handle the request by comparing the request media types with the media type (also known as Content-Type) supported by the View associated with each of its ViewResolvers. The first View in the list that has a compatible Content-Type returns the representation to the client. If a compatible view cannot be supplied by the ViewResolver chain, the list of views specified through the DefaultViews property is consulted. This latter option is appropriate for singleton Views that can render an appropriate representation of the current resource regardless of the logical view name. The Accept header can include wildcards (for example text/*), in which case a View whose Content-Type is text/xml is a compatible match.
See View Resolvers under MVC Config for configuration details.

 

어떠한 요청이 들어오면 해당 요청에 응답할 수 있는 모든 요청을 찾는다. 

accept-header와 view 타입을 비교해서 최종적으로 선택을 한다.

 

@Test
public void createUser_XML() throws Exception {
    //String userJson = "{\"userrname\":\"choi\", \}";
    JSONObject userJson = new JSONObject();
    userJson.put("username", "choi");
    userJson.put("password", "123");
    userJson.toString();

    mockMvc.perform(post("/users/create")
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_XML)
            .content(String.valueOf(userJson)))
            .andExpect(status().isOk())
            .andExpect(xpath("/User/name")
                    .string("choi"))
            .andExpect(xpath("User/password")
                    .string("123")
            );
}

JacksonHttpMessageConvertersConfiguration -> JacksonHttpMessageConvertersConfiguration

현재는 xml message를 converting 할 수 있는 converter가 없다

xmlMapper 클래스가 해당 classpath에 없기 때문

 

 

pom.xml 추가

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.10.1</version>
</dependency>

코드 참조

https://github.com/mike6321/Spring/tree/master/SpringBoot/SpringMvc

 

mike6321/Spring

Contribute to mike6321/Spring development by creating an account on GitHub.

github.com