一文读懂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.hqwc.cn/news/614825.html

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

相关文章

用c++实现串匹配问题、选择排序

5.2.2 串匹配问题 【问题】 给定两个字符串S和T&#xff0c;在主串S中查找子串T的过程称为串匹配(string matching,也称模式匹配&#xff09;&#xff0c;T称为模式。在文本处理系统、操作系统、编译系统、数据库系统以及 Internet 信息检索系统中&#xff0c;串匹配是使用最频…

【c++】优先级队列|反向迭代器(vector|list)

优先级队列的常用函数的使用 #include<iostream> #include<queue> using namespace std;int main() {priority_queue<int>st;st.push(1);st.push(7);st.push(5);st.push(2);st.push(3);st.push(9);while (!st.empty()){cout << st.top() << &qu…

Linux第89步_了解异步通知及其结构和函数

1、了解“异步通知” “异步通知”的核心就是信号。信号是采用软件模拟的“中断”&#xff0c;它由“驱动程序”主动向“应用程序”发送信号&#xff0c;并报告自己可以访问了&#xff0c;“应用程序”收到信号以后&#xff0c;就从“驱动设备”中读取或者写入数据。整个过程就…

数字化用户投稿发表论文

《数字化用户》是由国家新闻出版总署批准的正规期刊。本刊坚持科学发展观&#xff0c;响应我国数字化信息时代的方针、政策和发展战略&#xff1b;探讨数字化建设的规划、方案和成果&#xff1b;交流数字化技术和应用的实例和经验&#xff1b;推广数字新技术、科技新理念&#…

C盘满了怎么办,清理工具TreeSize

TreeSize是一款强大的磁盘空间分析工具&#xff0c;它可以帮助用户轻松地找出电脑中占用空间最多的文件和程序&#xff0c;从而让用户进行针对性地删除或卸载。 占用空间很小 下载链接&#xff1a;https://pan.quark.cn/s/bea23ed6b1d3

电动车新国标迎来修订机会,用户的真实需求能被满足吗?

文&#xff5c;新熔财经 作者&#xff5c;宏一 自2019年4月《电动自行车安全技术规范》发布至今&#xff0c;电动车的新国标标准已经实施5年&#xff0c;市场上的争议也此起彼伏地持续了5年。 因为新国标对电动车的各项技术标准提出的明确要求&#xff0c;其中&#xff0c;最…

Docker 学习笔记(七):介绍 Dockerfile 相关知识,使用 Dockerfile 构建自己的 centos 镜像

一、前言 记录时间 [2024-4-12] 系列文章简摘&#xff1a; Docker学习笔记&#xff08;二&#xff09;&#xff1a;在Linux中部署Docker&#xff08;Centos7下安装docker、环境配置&#xff0c;以及镜像简单使用&#xff09; Docker 学习笔记&#xff08;三&#xff09;&#x…

MathJax —— Vue3的使用方法

版本&#xff1a; mathjax3 需要实现效果 一、使用方式 1. index.html 中引入 <!-- 识别单行&#xff0c;行内&#xff0c;\( \)样式的公式 --><script>MathJax {tex: {inlineMath: [[$, $],[$$, $$], [\\(, \\)]]},};</script><script id"MathJ…

使用vite从头搭建一个vue3项目(二)创建目录文件夹以及添加vue-router

目录 一、创建 vue3 项目 vite-vue3-project-js二、创建项目目录三、创建Home、About组件以及 vue-router 配置路由四、修改完成后页面 一、创建 vue3 项目 vite-vue3-project-js 使用 vite 创建一个极简 vue3 项目请参考此文章&#xff1a;使用Vite创建一个vue3项目 下面是我…

顺序表(增删减改)+通讯录项目(数据结构)

什么是顺序表 顺序表和数组的区别 顺序表本质就是数组 结构体初阶进阶 系统化的学习-CSDN博客 简单解释一下&#xff0c;就像大家去吃饭&#xff0c;然后左边是苍蝇馆子&#xff0c;右边是修饰过的苍蝇馆子&#xff0c;但是那个好看的苍蝇馆子一看&#xff0c;这不行啊&a…

使用SquareLine Studio创建LVGL项目到IMX6uLL平台

文章目录 前言一、SquareLine Studio是什么&#xff1f;二、下载安装三、工程配置四、交叉编译 前言 遇到的问题&#xff1a;#error LV_COLOR_DEPTH should be 16bit to match SquareLine Studios settings&#xff0c;解决方法见# 四、交叉编译 一、SquareLine Studio是什么…

GEE数据集——巴基斯坦国家级土壤侵蚀数据集(2005 年和 2015 年)

简介 巴基斯坦国家级土壤侵蚀数据集&#xff08;2005 年和 2015 年&#xff09; 该数据集采用修订的通用土壤流失方程 (RUSLE)&#xff0c;并考虑了六个关键影响因素&#xff1a;降雨侵蚀率 (R)、土壤可侵蚀性 (K)、坡长 (L)、坡陡 (S)、覆盖管理 (C) 和保护措施 (P)&#xff…