How to implement custom endpoint in Spring Boot actuator to health - java

How would I implement a custom endpoint in SpringBoot to achieve the following:
http://localhost:8080/actuator/health/custom
where "custom" is the endpoint I want to implement that extends health.

That url is used to expose the Health checks for the application, if that is your situation, you only need to include the dependencies for the actuator :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
and you can see the status of the app.
If you need something more specific, only configure the dependency as you can see here .
Also you can override the behavior of the healthchecks but that dependency have a lot of functionality.

Related

How can I ensure that one spring-boot-starter is loaded before another?

I'm trying to ensure that one Spring Boot Starter is loaded before another. In this case, I have a custom spring boot starter written by my organization and the spring-boot-starter-data-jpa starter. It's crucial that the custom starter's bean is ran before spring-boot-starter-data-jpa is initialized. An example snippet of the dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-starter-my-customer-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
How would one go about this? The solution I'm currently considering is adding a #Primary annotation to the top of the bean initialized in my company's custom starter, not sure if this would work and looking for best practice here. The bean I'm attempting to load in the custom starter before spring-boot-starter-jpa's beans are loaded is responsible for setting proxy settings, therefore must be loaded first so that traffic to the database can be routed via proxy.
Not really sure as to what you are trying to achieve here, but a way of controlling bean creation could be done using the DependsOn annotation.
In you case, since you would like to initialize your custom bean before the actual database connection is made, I think that adding this to your database configuration class should be enough.

What is the actuator endpoint to which the togglz library exposes the edit options

I have recently being exploring the togglz library for feature management in Spring Boot application.
In the documentation here , its mentioned that I can use the actuator endpoint
http://localhost:8080/actuator/togglz/GREETING
to edit my features. But what is 'GREETING' here?
I tried with my feature name, but it didn't worked. It's also not mentioned in the documentation either.
Please help me out if someone has used this.
I am using the below dependency
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
<version>2.6.1.Final</version>
</dependency>
PS: I know about the console, but I don't want to use the console, I have to use the endpoint only to toggle the feature.
The actuator endpoint only allows you to change the state (i.e. true or false) of an existing feature by that same name. See the code's comment and implementation: https://github.com/togglz/togglz/blob/2.8.0/spring-boot/starter/src/main/java/org/togglz/spring/boot/actuate/TogglzEndpoint.java#L63

Is it necessary to add Spring Web when using Spring Actuator?

I'm trying to in corporate Spring Actuator to my application. I have added the dependency in my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
But I get a 404 when trying to access the /health endpoint. After looking online, I've read that I need to also have the spring-boot-starter-web dependency in my POM. I was under the assumption that I only need the actuator dependency in order to get it working
Yes web is needed if you want to access via HTTP (otherwise only JMX is available).
The documentation for actuator states
"Click Dependencies and select Spring Web and Spring Boot Actuator."

Don't understand how Spring (Boot) Data REST is told to create API endpoints

I have currently have an application that is using Spring Boot and Spring Data. I have my domain objects that were reverse engineered from my database and I have several repositories (classes). Each repository is an interface that extends the CrudRepository.
import org.springframework.data.repository.CrudRepository
interface MyDomainClassRepository extends CrudRepository<MyDomainClass, Integer> {
private MyDomainClass findByName(String name);
}
At this point I would create a service that would implement these items. The service would then be called by a REST controller.
I wanted to be able to have Spring create my REST API automatically if possible and I found the Spring Data REST project. I found this http://spring.io/guides/gs/accessing-data-rest/ and I can follow that guide, but I don't understand what is enabling the "REST APIs" to be created "automatically". I could understand it if the #RepositoryRestResource annotation caused the API to be created but in that guide it explicitly says
RepositoryRestResource is not required for a repository to be exported. It is only used to change the export details, such as using /people instead of the default value of /persons.
Does including in my POM file and rebuilding "automatically" allow Spring Data to create the REST endpoints?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
What keyword, section am I missing that makes it so the API endpoint is created automatically?
Spring Boot is opinionated. It has opinions like using tomcat as your application server or logback as your logging utility. When you pull in
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
It has opinions that when it detects (by classpath scanning) interfaces/classes that extend/implement Repository it will assume that those classes should be served as RESTful resources. RestRepository allows you to customize this behavior by changing the endpoint or not serving the resource at all (exported = false).
Spring-Data-REST does automatically setup the resource to handle GET/POST/PUT/DELETE requests. Are you familiar with REST/HTTP? Those would not be discrete endpoints, GET/PUT/POST/DELETE are http verbs, so there wouldn't be a resource/1/delete endpoint.
Spring Data Rest implements six controllers, in the package org.springframework.data.rest.webmvc:
RepositoryController: handles /
RepositoryEntityController: handles /{repository}
RepositoryPropertyReferenceController: handles /{repository}/{id}/{property} and /{repository}/{id}/{property}/{propertyId}
RepositorySearchController: handles /{repository}/search and /{repository}/search/{repoFunctionName}
ProfileController: handles /profile
RepositorySchemaController: handles /profile/{repository}
They are essentially the same as Spring MVC Controllers, except they are designed to work with Spring Data repositories in a general way
So, if you do GET /foo where foo is the path of your fooRepository, then Spring Data Rest will call RepositoryEntityController.getCollectionResource(), and invoke fooRepository.findAll(...), wrap the result in some HATEOAS objects, then marshal to JSON, using Jackson

How do I find a list of beans for a given Spring Boot class/package?

I am currently working with Spring Boot with a number of starter packs, such as the web MVC framework, Thymeleaf templates and security.
Each of these packages have a lot of different configuration options. I have mainly been using the source from the Auto-configuration package to figure out which beans need to be wired up and how to do that.
However, Is there any easy way to find a list of expected beans/classes that are needed by a given Spring package?
To view the beans you can you spring boot actuator
To Enable actuator, you can simply add the actuator starter dependency to your project.
For Gradle
compile("'org.springframework.boot:spring-boot-starter-actuator:1.3.1.RELEASE'
")
For Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
You can skip the version if you are using spring boot parent pom
Now you can rebuild your application make get request to http://server:port/beans if local and port is 8080 then
http://localhost:8080/beans
If you want to access using CURL command line tool you can
curl http://localhost:8080/beans
For more information visit
http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Categories