Skip to content
Advertisement

SpringBoot Actuator getting overshadowed by BaseService Servlet on same Port

We are Attempting to Migrate Our Existing Spring WebServices App to SpringBoot and ran into an issue for which we seek your advice.

We have a Base Service Servlet that disables the GET on the port that the App is deployed on for Security reasons this servlet returns 501 Unimplemented Response as follows:

 @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.warn("GET request received!");
        response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
    }
public abstract class BaseServiceServlet extends HttpServlet {
 ...
 } 
  
 public class ServiceServlet extends BaseServiceServlet {
 ...
 } 
  
  
  public class ServletInitializer extends SpringBootServletInitializer implements ServletContextInitializer { 
  @Override
    public void onStartup(ServletContext container) throws ServletException {
         
      ServletRegistration.Dynamic application = container
                .addServlet("ServiceServlet", new ServiceServlet()); 
      application.setLoadOnStartup(2);
      application.addMapping("/*");
        
    }
 }

Previously we had an old-fashioned HealthCheck JSP that we implemented. With the move to SpringBoot we are now using the SpringBoot Actuator.

But we find that if we set the Actuator health monitor port to the same one as the App when we try to monitor the health we get the 501 Unimplemented response.

Config is as follows:

# Spring-Boot Embedded Tomcat Config
server.port=8080
server.connection-timeout=10s
server.ssl.enabled=false
## Springboot based health monitor
management.endpoints.enabled-by-default=true
management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true
management.endpoints.web.cors.allowed-methods=GET
management.endpoint.beans.cache.time-to-live=20s
management.endpoints.web.base-path=/manage
management.server.port=8080
management.security.enabled=false 

One way we could get around this problem is if we changed the actuator health check port to something else that Works.

Question: How can we set the Actuator port to be the same as the App and make the actuator health check url which is something like http://localhost:8080/manage/health not return 501 Unimplemented from the Base Service Servlet ?

Advertisement

Answer

We can register DispatcherServlet with health endpoint in mapping before the ServiceServlet.

@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer implements ServletContextInitializer {
    @Autowired
    private DispatcherServlet dispatcherServlet;

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        ServletRegistration.Dynamic dispatcher =
                container.addServlet("dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/manage", "/manage/health");

        ServletRegistration.Dynamic application = container
                .addServlet("ServiceServlet", new ServiceServlet());
        application.setLoadOnStartup(2);
        application.addMapping("/*");
    }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement