Thursday 19 September 2019

Spring Boot Actuator: info Endpoint


‘info’ end point is used to provide information about the application (like application version, developer and tester details, contact information etc.,).

How to add information to info endpoint?
Any property that starts with info. is shown in actuator/info response.

For example, I added team name, size, developer, product owner, architect and testers details like below.

application.properties
info.team.name=Alaska
info.team.size=23
info.team.dev=krishna, ram, Sunil, Bikash
info.team.test=hari, pani, panth

info.version=1.23
info.product.owner=Ashwani
info.product.architect=Surav

Once you hit the endpoint ‘http://localhost:8080/actuator/info’, you will get below json response.
{
  "team": {
    "name": "Alaska",
    "size": "23",
    "dev": "krishna, ram, Sunil, Bikash",
    "test": "hari, pani, panth"
  },
  "version": "1.23",
  "product": {
    "owner": "Ashwani",
    "architect": "Surav"
  }
}

Find the below working application.


Step 1: Create new maven project ‘actuatorDemo’. Project structure looks like below.


Step 2: Update pom.xml with maven dependencies.
<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-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
 </dependency>

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

</dependencies>


pom.xml
<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>actuatorDemo</groupId>
 <artifactId>actuatorDemo</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.7.RELEASE</version>
  <relativePath />
 </parent>

 <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-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
  </dependency>

 </dependencies>

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


</project>

Step 3: Create a package ‘com.sample.app’ and define App.java like below.


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
 public static void main(String[] args) {
  
  SpringApplication.run(App.class, args);
 }
}

Step 4: Create a package ‘com.sample.app.controller’ and define HelloController like below.


HelloController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

 @RequestMapping("/")
 public String home() {
  return "Welcome to spring boot developement";
 }

}

Step 5: Create application.properties file under src/main/resources folder.


application.properties
spring.security.user.name=krishna
spring.security.user.password=password123
spring.security.user.roles = ["USER", "ADMIN"]

info.team.name=Alaska
info.team.size=23
info.team.dev=krishna, ram, Sunil, Bikash
info.team.test=hari, pani, panth

info.version=1.23
info.product.owner=Ashwani
info.product.architect=Surav


Total project structure looks like below.

Run App.java.


Open the url 'http://localhost:8080/actuator/info',  (it will redirect you to login page, enter user name as krishna and password as password123) you will get below response.
{
  "team": {
    "name": "Alaska",
    "size": "23",
    "dev": "krishna, ram, Sunil, Bikash",
    "test": "hari, pani, panth"
  },
  "version": "1.23",
  "product": {
    "owner": "Ashwani",
    "architect": "Surav"
  }
}


You can download complete working application from this link.




Previous                                                    Next                                                    Home

No comments:

Post a Comment