Skip to content

Commit a4f4301

Browse files
yasin3061maibin
authored andcommitted
BAEL-729 Adding custom info to actuator's /info endpoint (eugenp#1584)
* yasin.bhojawala@gmail.com Evaluation article on Different Types of Bean Injection in Spring * Revert "yasin.bhojawala@gmail.com" This reverts commit 963cc51. * Fixing compilation error and removing unused import * Introduction to Java9 StackWalking API - yasin.bhojawala@gmail.com Code examples for the article "Introduction to Java9 StackWalking API" * BAEL-608 Introduction to Java9 StackWalking API * BAEL-608 Introduction to Java9 StackWalking API changing the test names to BDD style * BAEL-608 Introduction to Java9 StackWalking API correcting the typo * BAEL-608 Introduction to Java9 StackWalking API improving method names * BAEL-608 Introduction to Java9 StackWalking API test method names improvements * BAEL-718 Quick intro to javatuples * merging pom from master * BAEL-722 Intro to JSONassert * BAEL-722 Intro to JSONassert Updated to 1.5.0 * BAEL-745 Quick Math.pow example * BAEL-729 Adding custom info to actuator's /info endpoint
1 parent 8da820b commit a4f4301

6 files changed

Lines changed: 92 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.boot.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.persistence.Table;
7+
8+
@Entity
9+
@Table(name = "users")
10+
public class User {
11+
12+
@Id
13+
@GeneratedValue
14+
private Integer id;
15+
private String name;
16+
private Integer status;
17+
18+
public Integer getId() {
19+
return id;
20+
}
21+
22+
public void setId(Integer id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public Integer getStatus() {
35+
return status;
36+
}
37+
38+
public void setStatus(Integer status) {
39+
this.status = status;
40+
}
41+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.boot.repository;
2+
3+
import org.baeldung.boot.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository("userRepository")
8+
public interface UserRepository extends JpaRepository<User, Integer> {
9+
public int countByStatus(int status);
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.endpoints.info;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.baeldung.boot.repository.UserRepository;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.actuate.info.Info;
9+
import org.springframework.boot.actuate.info.InfoContributor;
10+
import org.springframework.stereotype.Component;
11+
12+
@Component
13+
public class TotalUsersInfoContributor implements InfoContributor {
14+
15+
@Autowired
16+
UserRepository userRepository;
17+
18+
@Override
19+
public void contribute(Info.Builder builder) {
20+
Map<String, Integer> userDetails = new HashMap<>();
21+
userDetails.put("active", userRepository.countByStatus(1));
22+
userDetails.put("inactive", userRepository.countByStatus(0));
23+
24+
builder.withDetail("users", userDetails);
25+
}
26+
}

‎spring-boot/src/main/resources/application.properties‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ server.port=8080
22
server.contextPath=/springbootapp
33
management.port=8081
44
management.address=127.0.0.1
5-
5+
#debug=true
6+
spring.jpa.show-sql=true
7+
spring.jpa.hibernate.ddl-auto = update
68
endpoints.shutdown.enabled=true
79

810
endpoints.jmx.domain=Spring Sample Application
@@ -22,6 +24,7 @@ http.mappers.jsonPrettyPrint=true
2224
info.app.name=Spring Sample Application
2325
info.app.description=This is my first spring boot application G1
2426
info.app.version=1.0.0
27+
info.java-vendor = ${java.specification.vendor}
2528

2629
## Spring Security Configurations
2730
security.user.name=admin1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
insert into users values (1, 'Alex', 1);
2+
insert into users values (2, 'Bob', 1);
3+
insert into users values (3, 'John', 0);
4+
insert into users values (4, 'Harry', 0);
5+
insert into users values (5, 'Smith', 1);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create table USERS(
2+
ID int not null AUTO_INCREMENT,
3+
NAME varchar(100) not null,
4+
STATUS int,
5+
PRIMARY KEY ( ID )
6+
);

0 commit comments

Comments
 (0)