- 
      
 - 
        
Save x22x22/ca42f94a27850e7192413abef7503f0f to your computer and use it in GitHub Desktop.  
    Spring Boot 2 Custom Endpoint Sample
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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; | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?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> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment