Content Types

Dev.Hammy·2024년 4월 8일
0

반응형 스택에서 이에 상응하는 내용 보기

Spring MVC가 요청에서 요청된 미디어 유형(예: Accept 헤더, URL 경로 확장, 쿼리 매개변수 등)을 결정하는 방법을 구성할 수 있습니다.

기본적으로 Accept 헤더만 선택됩니다.

URL 기반 콘텐츠 유형 확인을 사용해야 하는 경우 경로 확장에 대한 쿼리 매개 변수 전략을 사용하는 것이 좋습니다. 자세한 내용은 접미사 matching접미사 matching과 RFD를 참조하세요.

Java 구성에서는 다음 예제와 같이 요청된 콘텐츠 유형 확인을 사용자 정의할 수 있습니다.

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		configurer.mediaType("json", MediaType.APPLICATION_JSON);
		configurer.mediaType("xml", MediaType.APPLICATION_XML);
	}
}

다음 예에서는 XML에서 동일한 구성을 달성하는 방법을 보여줍니다.

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
	<property name="mediaTypes">
		<value>
			json=application/json
			xml=application/xml
		</value>
	</property>
</bean>

0개의 댓글