Search code examples
javalocalhosthealth-monitoring

Return HTTP 200 on hardcoded endpoint in Java App


I have a standard Java app (not Spring Boot) and want it to return OK when I ping localhost:8080/health I am a complete beginner to anything web dev. Most of the resources online are for Spring Boot apps (which this isn't). Other StackOverflow posts say to use ServerSocket, but is there a higher level way to do this?

Would really appreciate if anyone could point me in the right direction :)


Solution

  • You have to create a class which implements the Sprint Boot's HealthIndicator and then create the method health() :

    @Component
    public class HealthIndicatorImpl implements HealthIndicator {
    
        @Override
        public Health health() {    
            return Health.up().build();
        }
    }
    

    This instruction return "OK" when you called the localhost:8080/health :

    return Health.up().build()
    

    In addition, if you want to show a KO, you can write :

    return Health.down().withDetail(/* code */, /* message */).build();