Home > Software design >  Issue with bodyToMono and READ_UNKNOWN_ENUM_VALUES_AS_NULL
Issue with bodyToMono and READ_UNKNOWN_ENUM_VALUES_AS_NULL

Time:03-10

I have bellow code

@Data
public class Test {
   private String name;
   private Type type;

   public enum Type {
       A("a"), B("b");

       private final String value;

       Type(String value) {
           this.value = value;
       }

       @JsonValue
       public String getValue() {
           return value;
       }
   }
}

        webClient
            .get()
            .uri(url)
            .exchangeToMono(response -> response.bodyToMono(Test.class))
            .map(test -> {

            });

And here is the config in application.yml

spring:
  jackson:
    deserialization:
      READ_UNKNOWN_ENUM_VALUES_AS_NULL: true

Here is the test response for the webClient call

{"name": "test", "type": "c"}

I expect test to be {"name": "test"} since READ_UNKNOWN_ENUM_VALUES_AS_NULL should ignore the unknown enum value "type": "c", but actually I got Mono.empty()

Any idea how to make READ_UNKNOWN_ENUM_VALUES_AS_NULL work with bodyToMono

CodePudding user response:

You need to configure WebClient codecs to use default Spring-managed Jackson ObjectMapper. Here is an example

WebClient webClient = WebClient.builder()
        .codecs(configurer -> {
            configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON));
            configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON));
        })
        .build();
  • Related