Home > Software engineering >  Spring Boot 2.1.3 custom actuator endpoint fails requiring all parameters to be in the query url
Spring Boot 2.1.3 custom actuator endpoint fails requiring all parameters to be in the query url

Time:09-15

I created a very simple actuator endpoint

@Component
@Endpoint(id="custom")
public class CustomActuatorEndpoint {

    @ReadOperation
    public Map<String, List<String>> invoke3params(String param1, String param2, String param3) {
        return new HashMap<>();
    }
}

And in my app it works okay with the endpoint /custom?param1=foo1&param2=foo2&param3=foo3

But if I try to invoke it with one param or two params or no params, like this: /custom?param1=foo1 or /custom I get HTTP 500 and an exception stating that the request is missing required parameters.

I tried to make them optional:

    @ReadOperation
    public Map<String, List<String>> invoke3params(Optional<String> param1, Optional<String> param2, Optional<String> param3) {
        return new HashMap<>();
    }

And I have the same result: HTTP 500 missing required parameters exception.

I also tried have different methods like this:

    @ReadOperation
    public Map<String, List<String>> invoke3params(String param1, String param2, String param3) {
        return new HashMap<>();
    }

    @ReadOperation
    public Map<String, List<String>> invoke1params(String param1) {
        return new HashMap<>();
    }

    @ReadOperation
    public Map<String, List<String>> invoke0params() {
        return new HashMap<>();
    }

And I tried overloading methods:

    @ReadOperation
    public Map<String, List<String>> invoke(String param1, String param2, String param3) {
        return new HashMap<>();
    }

    @ReadOperation
    public Map<String, List<String>> invoke(String param1) {
        return new HashMap<>();
    }

    @ReadOperation
    public Map<String, List<String>> invoke() {
        return new HashMap<>();
    }

Then the app fails to start with this exception:

2022-09-14T20:17:28.847Z ERROR --- [main] o.s.b.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pathMappedEndpoints' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints]: Factory method 'pathMappedEndpoints' threw exception; nested exception is java.lang.IllegalStateException: Unable to map duplicate endpoint operations: [web request predicate GET to path 'custom' produces: application/vnd.spring-boot.actuator.v2 json,application/json]

What am I missing? how do I have a query in the custom actuator endpoint? Thank you!

CodePudding user response:

You can use Spring Framework's @Nullable annotation:

Operations on an endpoint receive input via their parameters. When exposed via the web, the values for these parameters are taken from the URL’s query parameters and from the JSON request body. When exposed via JMX, the parameters are mapped to the parameters of the MBean’s operations. Parameters are required by default. They can be made optional by annotating them with @org.springframework.lang.Nullable.

  • Related