1

I have search some smilar question in stackoverflow. Referenced question is link .

I try to serialize a prometheus inner class called MetricFamilySamples in simple-client-0.9.0.jar. Package is io.prometheus.client.Collector$MetricFamilySamples. My serilize code is


 Enumeration<Collector.MetricFamilySamples> metricFamilySamplesEnumeration = CollectorRegistry.defaultRegistry
                .filteredMetricFamilySamples(metricQueryDTO.getParsedQueryStrSet());

            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

return objectMapper.writeValueAsString(metricFamilySamplesEnumeration));

I check the class and ensure the field is public. However, I got the following exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration["collectorIter"]->java.util.HashMap$KeyIterator["current"]->java.util.HashMap$Node["com.clougence.cloudcanal.sidecar.component.collect.WorkerStatCollector@6274670b"])

The WorkerStatCollector is a implemented collector extend prometheus Collector. How can I serialize the class with jackson? I use another json serialize tool called fastjson and it can give the result like following picture(part of the result of fastjson serilization). enter image description here

Kami Wan
  • 724
  • 2
  • 9
  • 23
  • you might want to look into different properties that can be set for the **ObjectMapper** – Vishal T Jul 30 '20 at 04:42
  • @VishalT What properties should I use? Actually, I am not sure what the root cause that make the serilization failed. – Kami Wan Jul 30 '20 at 07:01
  • The error shows you the configuration that needs to used.For full list [link](https://github.com/FasterXML/jackson-databind/wiki/Serialization-Features) – Vishal T Jul 30 '20 at 08:26
  • @VishalT The feature FAIL_ON_EMPTY_BEANS just ignore the fail exception. In my case, I want to know why my serilization fail. The exception happen when class field is not public or not have getter. In my case, the class's field is public. Ignore the exception and return empty json is not my expected result – Kami Wan Jul 30 '20 at 09:20

0 Answers0