Spring Boot 2.0 Not Registering Custom Actuator Endpoint

不羁的心 提交于 2020-01-21 14:39:26

问题


I've recently upgraded a project from Spring Boot 1.5.1 to 2.0.0 and our custom actuator endpoint is not getting registered. I've tried the following endpoint migration guides (docs.spring.io, github, and spring.io) to migrate our custom endpoint with the new approach but it doesn't work.

Here is a simplified endpoint I'm trying to register:

@Component
@Endpoint(id = "time")
public class TimeEndpoint {
    @ReadOperation
    public Map<String, Object> getTimeInfo() {
        return new HashMap<String, Object>() {{
            put("time", DateTime.now(UTC));
        }};
    }
}

I've even tried removing the @Component from the class and registering it as a bean in my main @Configuration class like the following:

@Bean
public TimeEndpoint timeEndpoint() {
    return new TimeEndpoint();
}

When I build the project and start up, I see it registering the /health, /info, and /actuator endpoints, but not my /time endpoint. Attempting to go to /time or /actuator/time results in a 404.

WebMvcEndpointHandlerMapping - Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
WebMvcEndpointHandlerMapping - Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
WebMvcEndpointHandlerMapping - Mapped "{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

I even tried upgrading to 2.0.1, but that didn't help either.

What am I missing?


回答1:


I believe, the answer by Sharan De Silva is not correct. I just spent several hours on a identical issue, because the property name is different. Should be:

management:
  endpoints:
    web:
      exposure:
        include: 'health,info,metrics,customendpoint'

Here's the official reference: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#production-ready-endpoints-exposing-endpoints




回答2:


You need to expose your custom endpoint by adding this to your application.yml file.

management:
  endpoints:
    web:
      expose: 'customendpoint,info,health'
      exclude: env

If you want to expose all your endpoints then change it as expose: '*'



来源:https://stackoverflow.com/questions/49700803/spring-boot-2-0-not-registering-custom-actuator-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!