added custom serializer

This commit is contained in:
Francesco Mangiacrapa 2024-10-18 15:36:16 +02:00
parent 6e608191df
commit af16e3b200
2 changed files with 16 additions and 5 deletions

View File

@ -5,11 +5,15 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
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.MongoHealthCheck;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
@Path("/health")
@ -18,10 +22,13 @@ public class GeoportalHealth {
@GET
@Path("")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
public HealthCheckResponse serviceCheck() {
@Produces({ MediaType.APPLICATION_JSON })
public String serviceCheck() throws JsonProcessingException {
log.info("serviceCheck called");
return new GeoportalHealthCheck().call();
HealthCheckResponse response = new GeoportalHealthCheck().call();
String json = healthCheckSerializer(response);
log.info("serviceCheck response is {}", json);
return json;
}
@GET
@ -36,4 +43,10 @@ public class GeoportalHealth {
return new MongoHealthCheck(context).call();
}
private String healthCheckSerializer(HealthCheckResponse response) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// Serializes HealthCheckResponse in JSON
return mapper.writeValueAsString(response);
}
}

View File

@ -19,8 +19,6 @@ public class GeoportalHealthCheck implements HealthCheck {
public HealthCheckResponse call() {
log.info(GeoportalHealthCheck.class.getSimpleName() + " call");
HealthCheckResponse response = HealthCheckResponse.named(SERVICE_NAME).state(true).withData("serviceStatus", "healthy").build();
log.info("Generated HealthCheckResponse: " + response.toString());
log.info("Generated HealthCheckResponse map: " + response.getData()!=null?response.getData().toString():"");
return response;
}