Spring Boot Actuator 中的自定义端点
Custom endpoint in Spring Boot Actuator
我正在尝试为 Spring 启动应用程序编写自定义端点。我编写了如下自定义端点实现。我没有包括额外的东西,比如导入以减少代码的大小。
@Component
public class TestendPoint implements Endpoint<List<String>>{
public String getId() {
return "test";
}
public List<String> invoke() {
List<String> test= new ArrayList<String>();
test.add("Details 1");
test.add("Details 2");
return serverDetails;
}
public boolean isEnabled() {
return true;
}
public boolean isSensitive() {
return false;
}
}
编写完上述代码后,我重新启动了应用程序并尝试从 /test 访问端点。但是,端点不可用。下面是Spring开机启动应用
@Configuration
@EnableAutoConfiguration
public class Application{
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
除此之外,我拥有 运行 一个 Spring 引导执行器的一切。我可以访问默认端点,如 /info、/metrics 等。
如果我在这里遗漏了什么,你能分享你的知识吗?我假设自定义端点 class 将在开发人员没有进一步配置的情况下加载。
我已经解决了这个问题。首先,我没有在应用程序中包含@ComponentScan 注释class。或者我应该有@SpringBoot 应用程序。当我添加注释时,我开始收到以下异常:
Caused by: java.lang.IllegalStateException: Could not evaluate condition owing to internal class not found. This can happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:50)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:92)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:79)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:62)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:361)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:345)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:278)
... 18 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:308)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:129)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.anyMatches(SpringBootCondition.java:112)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$DatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:333)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:44)
... 24 more
这是包的原因。我试过从根包执行 main class 。我不确定这是否是 spring 启动应用程序中的错误,我会在他们的站点上记录一个 JIRA。一旦我添加了更多级别的包结构,它就开始正常工作了。
我正在尝试为 Spring 启动应用程序编写自定义端点。我编写了如下自定义端点实现。我没有包括额外的东西,比如导入以减少代码的大小。
@Component
public class TestendPoint implements Endpoint<List<String>>{
public String getId() {
return "test";
}
public List<String> invoke() {
List<String> test= new ArrayList<String>();
test.add("Details 1");
test.add("Details 2");
return serverDetails;
}
public boolean isEnabled() {
return true;
}
public boolean isSensitive() {
return false;
}
}
编写完上述代码后,我重新启动了应用程序并尝试从 /test 访问端点。但是,端点不可用。下面是Spring开机启动应用
@Configuration
@EnableAutoConfiguration
public class Application{
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
除此之外,我拥有 运行 一个 Spring 引导执行器的一切。我可以访问默认端点,如 /info、/metrics 等。
如果我在这里遗漏了什么,你能分享你的知识吗?我假设自定义端点 class 将在开发人员没有进一步配置的情况下加载。
我已经解决了这个问题。首先,我没有在应用程序中包含@ComponentScan 注释class。或者我应该有@SpringBoot 应用程序。当我添加注释时,我开始收到以下异常:
Caused by: java.lang.IllegalStateException: Could not evaluate condition owing to internal class not found. This can happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:50)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:92)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:79)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:62)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:361)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:345)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:278)
... 18 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:308)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:129)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.anyMatches(SpringBootCondition.java:112)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$DatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:333)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:44)
... 24 more
这是包的原因。我试过从根包执行 main class 。我不确定这是否是 spring 启动应用程序中的错误,我会在他们的站点上记录一个 JIRA。一旦我添加了更多级别的包结构,它就开始正常工作了。