I use the MicroProfile REST Client in Quarkus and have declared quarkus-rest-client-reactive-jackson as dependency. For the invocation I need run my own code to serialize my data into JSON. I’ve built a custom serializer extending StdSerializer and registered it via ObjectMapperCustomizer.
I’ve followed the instructions in this documentation:
But my code is never invoked. The code in my ObjectMapperCustomizer in run and the constructors of my serializer are run, but the serialize methhod is never invoked.
public class HighlightSerializer extends StdSerializer<Highlight> {
public HighlightSerializer() {
this(null);
System.out.println("constuctor");
}
public HighlightSerializer(Class<Highlight> t) {
super(t);
System.out.println("constuctor");
}
@Override
public void serialize(Highlight value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
System.out.println("serialize");
jsonGenerator.writeStringField("niklas", "niklas");
}
@Singleton
public class RegisterCustomModuleCustomizer implements ObjectMapperCustomizer {
@Override
public int priority() {
return MINIMUM_PRIORITY;
}
public void customize(ObjectMapper mapper) {
System.out.println("customize");
SimpleModule module = new SimpleModule();
module.addSerializer(Highlight.class, new HighlightSerializer());
mapper.registerModule(module);
}
}
Here are pointers to my code:
- Desired JSON that needs to be created with a variable field name: https://github.com/nheidloff/question-answering/blob/299c9131310d5cbfd8fe3620a07738f973852c64/service/src/main/java/com/ibm/question_answering/elasticsearch/AskElasticService.java#L129
- Serializer that is not invoked: https://github.com/nheidloff/question-answering/blob/299c9131310d5cbfd8fe3620a07738f973852c64/service/src/main/java/com/ibm/question_answering/elasticsearch/HighlightSerializer.java#L21
- Attempt to register the serializer: https://github.com/nheidloff/question-answering/blob/299c9131310d5cbfd8fe3620a07738f973852c64/service/src/main/java/com/ibm/question_answering/elasticsearch/RegisterCustomModuleCustomizer.java#L18
Does anyone know how to fix this?
Thanks
Update May 15th:
I’ve simplified the code. The issue is related to the REST Client. The same serialization code works in a REST endpoint, but not when invoking another REST API. So the issue is not related to using different serializers together and not a basic issue in my code.
https://github.com/nheidloff/quarkus-stackoverflow-76228719
Any ideas how to fix this?
2
Answers
Au, this is embarrassing. I simply forgot to create the object. Can't make this up.
I created a sample app using your code and it’s functioning properly. https://github.com/bgizdov/quarkus-reactive-jackson-serializer
One modification to make is adding "jsonGenerator.writeStartObject();" before writing fields in the serialize method.
The endpoint http://localhost:8080/hello/highlight returns:
If your project is not working, consider the following:
Here is my log: