RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
SpringBoot2.x怎么自定义Endpoint

小编给大家分享一下Spring Boot2.x怎么自定义Endpoint,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联专注于汝阳企业网站建设,响应式网站建设,电子商务商城网站建设。汝阳网站建设公司,为汝阳等地区提供建站服务。全流程按需设计网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

概述

在使用Spring Boot的时候我们经常使用actuator,健康检查,bus中使用/refresh等。这里记录如何使用注解的方式自定义Endpoint。可用于满足一些服务状态监控,或者优雅停机等。

准备

Spring Boot项目,pom中加入:


  org.springframework.boot
  spring-boot-starter-actuator

编写自定义Endpoint

@Configuration
@Endpoint(id = "my-endpoint")
public class MyEndpoint {
  @ReadOperation
  public Map endpoint() {
    Map map = new HashMap<>(16);
    map.put("message", "this is my endpoint");
    return map;
  }
}

1.配置

management.endpoints.web.exposure.include=my-endpoint

2.启动&测试

启动后可以看到日志:

Mapped "{[/actuator/my-endpoint],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)

3.注意

  • @EndPoint中的id不能使用驼峰法,需要以-分割。

  • @Spring Boot会去扫描@EndPoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分别对应生成Get/Post/Delete的Mapping。注解中有个produces参数,可以指定media type, 如:application/json等。

以上是“Spring Boot2.x怎么自定义Endpoint”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章名称:SpringBoot2.x怎么自定义Endpoint
网站路径:http://scyingshan.cn/article/ihdjic.html