Spring Boot 2.0自定义Actuator端点无法注册问题求助
Hey there, let's dig into why your custom Actuator endpoint isn't registering after upgrading to Spring Boot 2.0.0. I've run into similar snags before, so here are the most common fixes to check:
1. Verify Your Actuator Dependencies
First, make sure you're using the correct 2.x Actuator dependency. Spring Boot 2.x restructured the Actuator starters, so double-check your build config:
For Maven (pom.xml):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.0.RELEASE</version> </dependency>
For Gradle (build.gradle):
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.0.0.RELEASE'
2. Expose the Custom Endpoint in Configuration
Spring Boot 2.x hides most endpoints by default for security. You need to explicitly expose your time endpoint. Add these properties to your config file:
application.properties
# Confirm the endpoint is enabled (it's on by default, but good to explicit) management.endpoint.time.enabled=true # Expose it via web access management.endpoints.web.exposure.include=time # Use `include=*` if you want all endpoints exposed (not recommended for production)
application.yml
management: endpoint: time: enabled: true endpoints: web: exposure: include: time
3. Confirm Component Scanning Covers Your Endpoint
Your TimeEndpoint uses @Component, but ensure its package falls within Spring's component scan scope. If your main application class is in com.example.myapp, your endpoint should be in a subpackage like com.example.myapp.endpoints (Spring scans the main class's package and subpackages by default). If it's outside this range, add @ComponentScan(basePackages = "com.your.endpoint.package") to your main application class.
4. Double-Check Your Endpoint Code
Let's make sure your code is complete and follows 2.x conventions. Here's a polished, working version of your endpoint:
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Component @Endpoint(id = "time") public class TimeEndpoint { @ReadOperation public Map<String, Object> getTimeInfo() { Map<String, Object> timeInfo = new HashMap<>(); timeInfo.put("currentTimeMillis", System.currentTimeMillis()); timeInfo.put("serverTimeZone", System.getProperty("user.timezone")); return timeInfo; } }
5. Clean Up Leftover 1.x Code
If you still have any deprecated 1.x Actuator classes (like those using @MvcEndpoint or @EndpointWebExtension from old code), they might conflict with your new endpoint. Fully remove any leftover 1.x endpoint-related code and annotations.
Once you've checked these steps, restart your app and test accessing http://localhost:8080/actuator/time (adjust port if you've changed it). You should see your time info JSON response.
内容的提问来源于stack exchange,提问作者james.lorenzen

