Skip to content

@JsonComponent (JsonSerializer) not picked up for WebFlux serialization  #9485

Closed
@michael-simons

Description

@michael-simons

I have a @JsonComponent that extends JsonSerializer:

@JsonComponent
public class SomeObjectSerializer extends JsonSerializer<SomeObject> {

    @Override
    public void serialize(SomeObject value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        gen.writeStartObject();
        gen.writeObjectField("a", value.getA());
        gen.writeEndObject();
    }
}

for this object:

public class SomeObject {
    public final String a;
    
    public final String b;

    public SomeObject(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }
  }

Using Spring Web MVC it gets picked up automatically in Spring Boot, so that a controller returning SomeObject as JSON renders {"a":"aa"}.

However when used with WebFlux, I see SomeObjectSerializer initialized but never used, the following controller

@RestController
public class SomeController {
    @GetMapping("/someObject")
    public Mono<SomeObject> getSomeObject() {
        return Mono.just(new SomeObject("aa", "bb"));
    }
}

always renders {"a":"aa","b":"bb"}

My expectation is the serializer picked up as one is used to with Web MVC. The docs (neither Spring Framework or Boot) give me any indication that it should not be the case with the new Jackson2JsonDecoder and encoder classes.

Demo project with test is attached.
InitializrSpringbootProject.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions