gpt4 book ai didi

java - 如何禁用 Spring Actuators 的内容协商?

转载 作者:行者123 更新时间:2023-12-05 08:06:44 33 4
gpt4 key购买 nike

我想在调用执行器端点 /info/health 时禁用 Content-Negotiation

这是我的配置文件

@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 中触发相同的 url 时,我收到了有效的响应。

在我的例子中,应该在没有标题的情况下调用执行器(没有 -H 'Accept: ...')

最佳答案

很遗憾,我只能提供一个次优的解决方案。

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.defaultContentTypeStrategy((webRequest) -> {
final String servletPath = ((HttpServletRequest) webRequest.getNativeRequest()).getServletPath();
final MediaType defaultContentType = Arrays.asList("/info", "/health").contains(servletPath)
? MediaType.APPLICATION_JSON : MediaType.APPLICATION_XML;
return Collections.singletonList(defaultContentType);
});
}

如果 /info/health 端点被调用,则返回 application/json。对于所有其他请求,使用默认的 application/xml

关于java - 如何禁用 Spring Actuators 的内容协商?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58999106/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com