我想在调用执行器端点/info
和/health
时禁用内容协商
这是我的配置文件
@Configuration
public class InterceptorAppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
}
}
当我curl http://localhost:8081/health
时
我收到:
DefaultHandlerExceptionResolver Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
但是,当我在Chrome中启动相同的网址时,我会收到有效的回复。
在我的情况下,执行器应在没有标题的情况下被调用(没有-H'Accept:...')
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
configurer.defaultContentTypeStrategy(new ContentNegotiationStrategy() {
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException {
// If you want handle different cases by getting header with webRequest.getHeader("accept")
return Arrays.asList(MediaType.APPLICATION_JSON);
}
});
}