Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable content negotiation for Spring Actuators?

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: ...')

like image 596
Alex Avatar asked Dec 10 '25 03:12

Alex


1 Answers

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.

like image 116
Sven Döring Avatar answered Dec 12 '25 18:12

Sven Döring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!