skip to Main Content

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:

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


  1. Chosen as BEST ANSWER

    Au, this is embarrassing. I simply forgot to create the object. Can't make this up.

    output.highlight = new Highlight(); 
    

  2. 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.

      @Override
      public void serialize(Highlight value, JsonGenerator jsonGenerator, SerializerProvider provider)
          throws IOException {
        System.out.println("serialize");
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("foo_bar", "fooobar");
        jsonGenerator.writeStringField("niklas", "niklas");
      }
    

    The endpoint http://localhost:8080/hello/highlight returns:

    {
        "foo_bar":"fooobar",
        "niklas":"niklas"
    }
    

    If your project is not working, consider the following:

    • Check if you have another Jackson object mapper customizer.
    • Make sure the object mapper is not deregistered.
    • Check logs if the bean is not removed from the project as unused, to prevent this try adding "@Unremovable".

    Here is my log:

    customize
    constuctor
    constuctor
    __  ____  __  _____   ___  __ ____  ______ 
     --/ __ / / / / _ | / _ / //_/ / / / __/ 
     -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /    
    --________/_/ |_/_/|_/_/|_|____/___/   
    2023-05-13 03:07:15,961 INFO  [io.quarkus] (Quarkus Main Thread) code-with-quarkus 1.0.0-SNAPSHOT on JVM (powered by Quarkus 2.16.7.Final) started in 1.340s. Listening on: http://localhost:8080
    2023-05-13 03:07:15,965 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
    2023-05-13 03:07:15,965 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy-reactive, resteasy-reactive-jackson, smallrye-context-propagation, vertx]
    serialize
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search