如何禁用弹簧执行器的内容协商?

问题描述 投票:0回答:1

我想在调用执行器端点/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:...')

java spring-boot spring-boot-actuator content-negotiation
1个回答
0
投票
@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); } }); }
© www.soinside.com 2019 - 2024. All rights reserved.