added custom HealthCheckResponseSerializer
This commit is contained in:
parent
af16e3b200
commit
e7d6a2d707
|
@ -9,10 +9,12 @@ import javax.ws.rs.core.Response;
|
|||
|
||||
import org.eclipse.microprofile.health.HealthCheckResponse;
|
||||
import org.gcube.application.geoportal.service.rest.health.GeoportalHealthCheck;
|
||||
import org.gcube.application.geoportal.service.rest.health.HealthCheckResponseSerializer;
|
||||
import org.gcube.application.geoportal.service.rest.health.MongoHealthCheck;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
@ -20,32 +22,47 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
public class GeoportalHealth {
|
||||
|
||||
private ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public GeoportalHealth() {
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(HealthCheckResponse.class, new HealthCheckResponseSerializer());
|
||||
mapper.registerModule(module);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("")
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String serviceCheck() throws JsonProcessingException {
|
||||
log.info("serviceCheck called");
|
||||
public Response serviceCheck() throws JsonProcessingException {
|
||||
log.debug("serviceCheck called");
|
||||
HealthCheckResponse response = new GeoportalHealthCheck().call();
|
||||
String json = healthCheckSerializer(response);
|
||||
log.info("serviceCheck response is {}", json);
|
||||
return json;
|
||||
return Response.ok().entity(json).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/mongo")
|
||||
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
|
||||
public HealthCheckResponse databaseCheck(@QueryParam("context") String context) {
|
||||
log.info("databaseCheck called in the context {}", context);
|
||||
if (context == null)
|
||||
return HealthCheckResponse.named("databaseCheck")
|
||||
public Response databaseCheck(@QueryParam("context") String context) throws JsonProcessingException {
|
||||
log.debug("databaseCheck called in the context {}", context);
|
||||
if (context == null) {
|
||||
HealthCheckResponse response = HealthCheckResponse.named("databaseCheck")
|
||||
.withData("context", "is required parameter (e.g. context=/gcube/devsec/devVRE)").down().build();
|
||||
String json = healthCheckSerializer(response);
|
||||
log.info("databaseCheck error response is {}", json);
|
||||
// Bad request
|
||||
return Response.status(400).entity(json).build();
|
||||
}
|
||||
|
||||
return new MongoHealthCheck(context).call();
|
||||
HealthCheckResponse response = new MongoHealthCheck(context).call();
|
||||
String json = healthCheckSerializer(response);
|
||||
log.info("databaseCheck response is {}", json);
|
||||
return Response.ok().entity(json).build();
|
||||
}
|
||||
|
||||
private String healthCheckSerializer(HealthCheckResponse response) throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
// Serializes HealthCheckResponse in JSON
|
||||
// Serializes HealthCheckResponse in JSON with custom HealthCheckResponseSerializer
|
||||
return mapper.writeValueAsString(response);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import org.eclipse.microprofile.health.Readiness;
|
|||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@Readiness
|
||||
@Liveness
|
||||
@Slf4j
|
||||
|
@ -18,7 +17,8 @@ public class GeoportalHealthCheck implements HealthCheck {
|
|||
@Override
|
||||
public HealthCheckResponse call() {
|
||||
log.info(GeoportalHealthCheck.class.getSimpleName() + " call");
|
||||
HealthCheckResponse response = HealthCheckResponse.named(SERVICE_NAME).state(true).withData("serviceStatus", "healthy").build();
|
||||
HealthCheckResponse response = HealthCheckResponse.named(SERVICE_NAME).state(true)
|
||||
.withData("serviceStatus", "healthy").build();
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package org.gcube.application.geoportal.service.rest.health;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.microprofile.health.HealthCheckResponse;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class HealthCheckResponseSerializer extends JsonSerializer<HealthCheckResponse> {
|
||||
|
||||
@Override
|
||||
public void serialize(HealthCheckResponse response, JsonGenerator gen, SerializerProvider serializers)
|
||||
throws IOException {
|
||||
gen.writeStartObject();
|
||||
if (response.getName() != null)
|
||||
gen.writeStringField("name", response.getName());
|
||||
if (response.getState() != null)
|
||||
gen.writeStringField("state", response.getState().toString());
|
||||
|
||||
response.getData().ifPresent(data -> {
|
||||
try {
|
||||
gen.writeObjectFieldStart("data");
|
||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
gen.writeObjectField(entry.getKey(), entry.getValue());
|
||||
}
|
||||
gen.writeEndObject();
|
||||
} catch (IOException e) {
|
||||
log.warn("Error on serializing the data field", e);
|
||||
}
|
||||
});
|
||||
|
||||
gen.writeEndObject();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package org.gcube.application.geoportal.service.health;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
import org.eclipse.microprofile.health.HealthCheckResponse;
|
||||
import org.gcube.application.geoportal.service.rest.health.HealthCheckResponseSerializer;
|
||||
|
||||
public class HealthCheckSerializer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
HealthCheckResponse response = HealthCheckResponse.named("geooportal-service")
|
||||
.state(true)
|
||||
.withData("status", "healthy")
|
||||
.build();
|
||||
|
||||
// Configura ObjectMapper con il serializer personalizzato
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(HealthCheckResponse.class, new HealthCheckResponseSerializer());
|
||||
mapper.registerModule(module);
|
||||
|
||||
// Serializza l'oggetto HealthCheckResponse in JSON
|
||||
String json = mapper.writeValueAsString(response);
|
||||
|
||||
// Stampa il JSON serializzato
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue