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

本文详细解读了SpringBoot中的WebEndpointProperties类,介绍了如何配置Web端点属性,包括basePath、exposure策略(include和exclude)以及pathMapping。并提供了配置文件示例和测试Demo,展示了如何通过配置文件绑定和验证这些设置。
摘要由CSDN通过智能技术生成

前言

对于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等类

截图如下:

在这里插入图片描述

  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值