一文读懂Java中的WebEndpointProperties类(附Demo)

目录

  • 前言
  • 1. 基本知识
  • 2. Demo
  • 3. 彩蛋

前言

对于Java的相关知识,推荐阅读:java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)

1. 基本知识

Spring Boot 的配置类 WebEndpointProperties,用于配置 Web 端点(endpoints)的相关属性

先看其源码类:

package org.springframework.boot.actuate.autoconfigure.endpoint.web;import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@Configu
rationProperties(prefix = "management.endpoints.web")
public class WebEndpointProperties {private final Exposure exposure = new Exposure();/*** Base path for Web endpoints. Relative to server.servlet.context-path or* management.server.servlet.context-path if management.server.port is configured.*/private String basePath = "/actuator";/*** Mapping between endpoint IDs and the path that should expose them.*/private final Map<String, String> pathMapping = new LinkedHashMap<>();public Exposure getExposure() {return this.exposure;}public String getBasePath() {return this.basePath;}public void setBasePath(String basePath) {Assert.isTrue(basePath.isEmpty() || basePath.startsWith("/"), "Base path must start with '/' or be empty");this.basePath = cleanBasePath(basePath);}private String cleanBasePath(String basePath) {if (StringUtils.hasText(basePath) && basePath.endsWith("/")) {return basePath.substring(0, basePath.length() - 1);}return basePath;}public Map<String, String> getPathMapping() {return this.pathMapping;}public static class Exposure {/*** Endpoint IDs that should be included or '*' for all.*/private Set<String> include = new LinkedHashSet<>();/*** Endpoint IDs that should be excluded or '*' for all.*/private Set<String> exclude = new LinkedHashSet<>();public Set<String> getInclude() {return this.include;}public void setInclude(Set<String> include) {this.include = include;}public Set<String> getExclude() {return this.exclude;}public void setExclude(Set<String> exclude) {this.exclude = exclude;}}}

解读上述源码的大致细节

  • @ConfigurationProperties(prefix = "management.endpoints.web"):注解表明这个类将会绑定以 management.endpoints.web 开头的配置属性
    配置文件(比如 application.propertiesapplication.yml)中,可以设置以 management.endpoints.web 为前缀的属性,Spring Boot 将会自动将这些属性注入到这个类的实例中

  • Exposure 内部静态类:定义 Web 端点的暴露(exposure)策略,包含了两个属性 include 和 exclude,分别表示应该包含哪些端点和排除哪些端点

  • basePath 属性:指定 Web 端点的基本路径,默认值为 "/actuator",所有的端点都会在 "/actuator" 这个路径下暴露。

  • pathMapping 属性:自定义端点的路径映射,可以将端点 ID 映射到自定义的路径上

一般接口的使用方式可以使用配置文件(以下为例子)

management.endpoints.web.base-path=/custom-path
management.endpoints.web.exposure.include=health,info
management.endpoints.web.path-mapping.health=/custom-health

2. Demo

以下Demo为单独test文件下的测试,方便测试类以及接口的使用

import java.util.HashMap;
import java.util.Map;import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.util.Assert;public class test {public static void main(String[] args) {testBasePathValidation();testInvalidBasePathValidation();}public static void testBasePathValidation() {Map<String, Object> properties = new HashMap<>();properties.put("management.endpoints.web.base-path", "/actuator");properties.put("management.endpoints.web.exposure.include", "health,info");try {WebEndpointProperties webEndpointProperties = bindProperties(properties);System.out.println("Base path: " + webEndpointProperties.getBasePath());} catch (BindValidationException e) {e.printStackTrace();}}public static void testInvalidBasePathValidation() {Map<String, Object> properties = new HashMap<>();properties.put("management.endpoints.web.base-path", "actuator");properties.put("management.endpoints.web.exposure.include", "health,info");try {bindProperties(properties);} catch (BindValidationException e) {System.out.println("Invalid base path validation passed: " + e.getMessage());}}private static WebEndpointProperties bindProperties(Map<String, Object> properties) {ConfigurationPropertySource source = new MapConfigurationPropertySource(properties);WebEndpointProperties webEndpointProperties = new WebEndpointProperties();webEndpointProperties.setBasePath("/actuator");webEndpointProperties = new WebEndpointPropertiesBinder().bind(webEndpointProperties, source);return webEndpointProperties;}private static class WebEndpointPropertiesBinder {public WebEndpointProperties bind(WebEndpointProperties properties, ConfigurationPropertySource source) {return properties;}}
}

截图如下:

在这里插入图片描述

3. 彩蛋

对于实战中的Demo
可以结合ServerWebExchange或者ServerHttpResponse等类

截图如下:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/813309.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

为什么市面上的数藏都长得很像?

为什么市面上的数藏都长得很像 一、NFT数藏的市场前景和概念二、目前市面上的数藏类型&#xff08;九类&#xff09;1. 头像类&#xff08;PFP&#xff09;2. 游戏类3. 艺术品/文物类4. 音乐类5. 影视类6. 演出类7. 门票类8. 体育类9. 品牌实物联名 三、各大数藏的相同点&#…

(弟)递归•斐波那契数、n的k次方

这里是目录哦 题目一&#xff1a;递归计算斐波那契数斐波那契数的定义代码运行截图递归过程递归停止条件&#xff08;1个参数&#xff09;✨非递归实现方法 题目二&#xff1a;递归实现n的k次方代码运行截图递归过程递归停止条件&#xff08;不止1个参数&#xff09;✨ 加油&am…

Java 中文官方教程 2022 版(四十九)

原文&#xff1a;docs.oracle.com/javase/tutorial/reallybigindex.html JAXB 示例 原文&#xff1a;docs.oracle.com/javase/tutorial/jaxb/intro/examples.html 以下部分描述如何使用包含在 JAXB RI 捆绑包中的示例应用程序。JAXB RI 捆绑包可从jaxb.java.net获取。下载并安装…

4月全新热文高科技,套用模板一键生成热文,没脑子拷贝,第二天出盈利

撰写热门文章&#xff0c;如今日头条或微信公众号文章&#xff0c;通常需要多长时间呢&#xff1f;从构思主题、搜集资料&#xff0c;到撰写成文&#xff0c;整个过程至少需要1小时&#xff0c;有时甚至可能需要2小时。 项目 地 址&#xff1a;laoa1.cn/1627.html 现在&…

位像素海外仓管理系统对接ERP系统教程,一对一教学

在海外仓管理过程中&#xff0c;对接ERP系统的重要性不言而喻的。这种对接不仅能让数据实时共享&#xff0c;还能让海外仓管理者优化整个供应链管理流程。 因此&#xff0c;今天小编就来教大家&#xff0c;海外仓仓库系统是怎么对接ERP物流系统的&#xff1f; 1.分析需求 在对接…

微信小程序兼容iphone适配安全区域

背景&#xff1a; 小程序页面底部在ios中会有小黑条遮挡 上代码&#xff1a; padding-bottom: constant(safe-area-inset-bottom); /* 兼容 iOS < 11.2 */ padding-bottom: env(safe-area-inset-bottom); /* 兼容 iOS > 11.2 */ 项目描述&#xff1a; 微信小程序是通过…

HTML 入门 ( 一 )

HTML文档创建 首先创建一个txt文本文档 修改文件后缀 HTML标签 标签结构 标签又称为元素,是HTML的基本组成单位分为: 双标签与单标签推荐小写标签名 结构: 双标签示例代码: <marquee> My name is Kvein. </marquee>单标签示例代码: <input>标签的并列与嵌…

Json和Qt中Json的使用学习笔记

视频链接 https://www.bilibili.com/video/BV1yf4y1A7ek/?p2&spm_id_frompageDriver&vd_sourcefa4ef8f26ae084f9b5f70a5f87e9e41b Json JSON是在网络传输中常用的数据格式&#xff0c;能将不同类型的数据统一起来&#xff0c;我们在发送数据前将不同类型的数据存入到…

Mysql入门基础教程(第一部分)

Mysql入门教程 MySQL基础教程解释了一些基本的SQL语句。如果这是您第一次使用关系数据库管理系统&#xff0c;本教程将为您提供使用MySQL数据库服务器所需的一切&#xff0c;例如查询数据&#xff0c;更新数据&#xff0c;管理数据库和创建表。 如果您已经熟悉其他关系数据库…

MySQL基础知识——MySQL日志

一条查询语句的执行过程一般是经过连接器、 分析器、 优化器、 执行器等功能模块&#xff0c; 最后到达存储引擎。 那么&#xff0c; 一条更新语句的执行流程又是怎样的呢&#xff1f; 下面我们从一个表的一条更新语句进行具体介绍&#xff1a; 假设这个表有一个主键ID和一个…

C++矩阵

C矩阵【基本】&#xff08;will循环&#xff09; #include<iostream> #include<string.h> using namespace std; int main() {int a[100][100]{0};int k 1;int i 0;int j 0;while(k<100){if(j>10){j0;i;}a[i][j]k;j;k;}i 0;j 0;while(true){if(i 9&am…

HarmonyOS实战开发-自定义通知角标、如何设定应用的桌面图标角标的功能。

介绍 本示例主要展示了设定应用的桌面图标角标的功能&#xff0c;使用ohos.notificationManager 接口&#xff0c;进行桌面角标的设置&#xff0c;通知的发送&#xff0c;获取等。 效果预览 使用说明 在使用本应用时&#xff0c;需安装并启动仿桌面应用&#xff1b;在主界面…

汇舟问卷:海外问卷调查怎么样?

越来越多的企业决定采用线上调查的方式来了解消费者的意愿。这种转变不仅反映了科技发展的必然趋势&#xff0c;也凸显了企业对市场动态和消费者需求的高度重视。 线上调查能够覆盖更广泛的受众群体&#xff0c;通过互联网的普及&#xff0c;企业可以轻松地触及全国各地的消费…

Clickhouse在CentOS8上安装部署

➜ wget https://github.com/ClickHouse/ClickHouse/releases/download/v24.3.2.23-lts/clickhouse-client-24.3.2.23.x86_64.rpm ➜ wget https://github.com/ClickHouse/ClickHouse/releases/download/v24.3.2.23-lts/clickhouse-server-24.3.2.23.x86_64.rpm ➜ wget htt…

C语言零碎知识点 02

i是先赋值在1&#xff0c;而1是先1再赋值 很多情况下逗号都只是单纯地用做分隔符 goto 语句用于一次性跳出多层循环&#xff0c;因为break只能跳出一层&#xff0c;其余情况下不要用&#xff0c;因为它会破坏代码本身的逻辑。 C语言中交换两个变量需要一个零时变量&#xff0c;…

《神奇女侠3:暗黑之魂》AI制作电影短片(上)

《神奇女侠3&#xff1a;暗黑之魂》AI制作电影短片&#xff08;上&#xff09; 黑暗滋生&#xff0c;世界沦陷&#xff0c;神奇女侠独战群魔&#xff0c;唤醒挚爱&#xff0c;守护最后的光明&#xff01; 《神奇女侠3&#xff1a;暗黑之魂》&#xff08;上&#xff09;电影开篇…

Pytorch深度学习完整CPU图像分类代码

1.准备数据集 train_data torchvision.datasets.CIFAR10("../data",trainTrue,transformtorchvision.transforms.ToTensor(),downloadTrue) test_data torchvision.datasets.CIFAR10("../data",trainFalse,transformtorchvision.transforms.ToTensor(),…

数据可视化插件echarts【前端】

数据可视化插件echarts【前端】 前言版权开源推荐数据可视化插件echarts一、如何使用1.1 下载1.2 找到js文件1.3 入门使用1.4 我的使用 二、前后端交互&#xff1a;入门demo2.1 前端htmljs 2.2 后端entitycontrollerservicemapper 三、前后端交互&#xff1a;动态数据3.1 前端j…

【08】c++运算符重载

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、加号运算符二、左移运算符三.递增或者递减运算符总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 例如&#xff1a;C运算符重载…

JVM性能调优——OOM分类及解决方案

文章目录 1、概述2、OOM案例1&#xff1a;堆内存溢出3、OOM案例2&#xff1a;元空间溢出4、OOM案例3:GC overhead limit exceeded5、OOM案例4&#xff1a;线程溢出6、小结 在工作中会经常遇到内存溢出(Out Of Memory,OOM)异常的情况&#xff0c;每当遇到OOM&#xff0c;总是让人…