I'd like to disable Content-Negotiation when actuator endpoints /info and /health are called
here is my configs file
@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);
}
}
When I curl http://localhost:8081/health
I receive:
DefaultHandlerExceptionResolver Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
However when I fire same url in Chrome, i receive a valid response.
In my case actuator should be called without headers (no -H 'Accept: ...')
Unfortunately I can only offer a suboptimal solution.
@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);
});
}
In case the /info or /health endpoints get called application/json is returned. On all other requests the default application/xml is used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With