diff --git a/util-health/src/main/java/ws/ament/hammock/health/HealthCheckServlet.java b/util-health/src/main/java/ws/ament/hammock/health/HealthCheckServlet.java index 800a6c9a..e2a693bb 100644 --- a/util-health/src/main/java/ws/ament/hammock/health/HealthCheckServlet.java +++ b/util-health/src/main/java/ws/ament/hammock/health/HealthCheckServlet.java @@ -29,6 +29,8 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MediaType; + import java.io.IOException; @WebServlet(urlPatterns = {"${health.servlet.uri}"}, name = "HealthServlet") @@ -39,13 +41,21 @@ public class HealthCheckServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - Jsonb jsonb = JsonbBuilder.newBuilder().build(); + resp.setContentType(MediaType.APPLICATION_JSON); HealthCheckModel healthCheckModel = healthCheckManager.performHealthChecks(); if (healthCheckModel.getOutcome().equalsIgnoreCase(HealthCheckResponse.State.UP.name())) { resp.setStatus(200); } else { resp.setStatus(503); } - jsonb.toJson(healthCheckModel, resp.getOutputStream()); + try (Jsonb jsonb = JsonbBuilder.newBuilder().build()) { + jsonb.toJson(healthCheckModel, resp.getOutputStream()); + } catch (IOException ex) { + // Re-throw IOException specifically to maintain existing functionality + throw ex; + } catch (Exception ex) { + // Convert other exceptions thrown by close() into a runtime exception + throw new RuntimeException(ex); + } } }