简体   繁体   中英

How migrate an actuator that extends AbstractMvcEndpoint to Spring Boot 2.x?

In Spring Boot 1.5.x, I created an actuator endpoint by extending AbstractMvcEndpoint . But this class no longer exists in Spring Boot 2.x.

How would I upgrade the following class?

@Configuration
@ManagementContextConfiguration
public class MyManagementController extends AbstractMvcEndpoint
{
    private static final Logger logger = LoggerFactory.getLogger( MyManagementController.class );

    public MyManagementController()
    {
        super( "/mypath, false, true );
    }

    @PostConstruct
    public void completeSetup()
    {
        setEnabled( true );
    }

    @GetMapping(
        value = "/" + PATH_PIECE_MANAGERS,
        produces = MediaType.APPLICATION_JSON_VALUE
    )
    @ResponseBody
    public String test(HttpServletRequest request) throws Exception
    {
        return "ok";
    }
}

You can use annotation @RestControllerEndpoint with spring-boot 2.x, here you can have your own request mapping along with HttpMethods. Here is a sample:

    @Component
    @RestControllerEndpoint(id = "remote")
    public class CustomActuator {
        @RequestMapping(value = {"/{actuatorInput}"}, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
        @ResponseBody
        public Map<String, Object> feature(@PathVariable("actuatorInput") String actuatorInput) {
            System.out.println("actuatorInput : " + actuatorInput);
            Map<String, Object> details = new HashMap<>();
            details.put("input", actuatorInput);
            return details;
        }
    }

Or, you can use @Endpoint(id = "endpoint") with which you can have @ReadOperation and @WriteOperation for GET and POST operations.

Here is a sample:

@Component
@Endpoint(id = "custom-health")
public class CustomHealthEndpoint {

    @ReadOperation
    public String customEndPointByName(@Selector String name) {
        return "custom-end-point : " + name;
    }
    @WriteOperation
    public void writeOperation(@Selector String name) {
        System.out.println("Write Operation! :: " + name);
    }
    @DeleteOperation
    public void deleteOperation(@Selector String name){
        System.out.println("Delete Operation! :: " + name);
    }
}

Refer: https://www.javadevjournal.com/spring-boot/spring-boot-actuator-custom-endpoint/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM