java Spring Boot 2自定义端点示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Spring Boot 2自定义端点示例相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>demo-endpoint</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo-endpoint</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
spring.application.name=demoendpoint
#spring.aop.proxy-target-class=true
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
logging.level.root=warn
logging.level.org.springframework=info
logging.level.com.example=info
server.port=8081
package com.example.demoendpoint;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
@Endpoint(id="my-params")
public class MyCustomEndpoint {
	  @ReadOperation
	  public Map<String, List<Parameter>> showParameters() {
			Map<String, List<Parameter>> params =  new HashMap<>();
			params.put("cat1", Arrays.asList(new Parameter("url", "toto"), new Parameter("user", "titi")));
			params.put("cat2", Arrays.asList(new Parameter("key.1", "secret"), new Parameter("key.2", "sE3reT")));
			params.put("cat3", Arrays.asList(new Parameter("other1", "foo"), new Parameter("other2", "bar")));
			return params;
	  }
	  @ReadOperation
	  public List<Parameter> showParametersByCategory(@Selector String category) {
			Assert.notNull(category, "category must not be null");
			switch (category) {
				case "cat1" : return Arrays.asList(new Parameter("url", "toto"), new Parameter("user", "titi"));
				case "cat2" : return Arrays.asList(new Parameter("key.1", "secret"), new Parameter("key.2", "sE3reT"));
				case "cat3" : return Arrays.asList(new Parameter("other1", "foo"), new Parameter("other2", "bar"));
				default : return Collections.emptyList();
			}
	  }
	  
	  private static class Parameter {
		private final String name;
		private final String value;

		Parameter(String name, String value) {
			this.name = name;
			this.value = value;
		}

		public String getName() {
			return name;
		}

		public String getValue() {
			return value;
		}
	  }
}

以上是关于java Spring Boot 2自定义端点示例的主要内容,如果未能解决你的问题,请参考以下文章

将 spring boot 执行器健康端点更改为自定义端点

Spring boot 使用@Endpoint注解自定义端点, 不能通过 Restfult 访问问题 原因分析

Spring boot 使用@Endpoint注解自定义端点, 不能通过 Restfult 访问问题 原因分析

Spring boot 使用@Endpoint注解自定义端点, 不能通过 Restfult 访问问题 原因分析

Spring boot 使用@Endpoint注解自定义端点, 不能通过 Restfult 访问问题 原因分析

在 Spring Boot 端点上使用自定义杰克逊映射器