如何迁移将AbstractMvcEndpoint扩展到Spring Boot 2.x的执行器?

时间:2018-12-20 17:45:30

标签: java spring spring-boot spring-boot-actuator

在Spring Boot 1.5.x中,我通过扩展AbstractMvcEndpoint创建了一个执行器端点。但是此类在Spring Boot 2.x中不再存在。

我如何升级以下课程?

@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";
    }
}

1 个答案:

答案 0 :(得分:2)

您可以在Spring-boot 2.x中使用注释@RestControllerEndpoint,在这里您可以将自己的请求映射与HttpMethods一起使用。这是一个示例:

    @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;
        }
    }

或者,您可以使用@Endpoint(id = "endpoint"),将@ReadOperation@WriteOperation用于GET和POST操作。

以下是示例:

@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);
    }
}

引用:cron