Skip to content

Jackson2JsonEncoder and Jackson2JsonDecoder should use provided mime types [SPR-15866] #20421

Closed
@spring-projects-issues

Description

@spring-projects-issues

Ricardo Lindooren opened SPR-15866 and commented

I'm using Webclient to consume a webservice that uses a legacy mime type for JSON: text/javascript.

This works when using the Webclient builder.

@Autowired
public ItunesAlbumServiceImpl(ObjectMapper mapper) {
    ExchangeStrategies strategies = ExchangeStrategies.builder().codecs(clientCodecConfigurer ->
        clientCodecConfigurer.customCodecs().decoder(
                new Jackson2JsonDecoder(mapper,
                        new MimeType("text", "javascript", StandardCharsets.UTF_8)))
    ).build();

    webClient = WebClient.builder()
            .exchangeStrategies(strategies)
            .baseUrl("https://itunes.apple.com")
            .build();
}

But when trying to configure this on application level it doesn't work.

@SpringBootApplication
public class SpringReactiveApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringReactiveApplication.class, args);
    }

    @Bean
    public CodecCustomizer jacksonLegacyJsonCustomizer(ObjectMapper mapper) {
        return (configurer) -> {
            MimeType textJavascript = new MimeType("text", "javascript", StandardCharsets.UTF_8);
            CodecConfigurer.CustomCodecs customCodecs = configurer.customCodecs();
            customCodecs.decoder(
                    new Jackson2JsonDecoder(mapper, textJavascript));
            customCodecs.encoder(
                    new Jackson2JsonEncoder(mapper, textJavascript));
        };
    }
}

While stepping through the code I noticed that the Jackson2JsonDecoder.getDecodableMimeTypes() and Jackson2JsonEncoder.getEncodableMimeTypes() methods always return the default JSON mime types, not the one(s) provided as constructor argument.

public class Jackson2JsonDecoder extends AbstractJackson2Decoder {

    public Jackson2JsonDecoder() {
        super(Jackson2ObjectMapperBuilder.json().build());
    }

    public Jackson2JsonDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
        super(mapper, mimeTypes);
    }

    @Override
    public List<MimeType> getDecodableMimeTypes() {
        return JSON_MIME_TYPES;
    }
}

Causing Jackson2CodecSupport.supportsMimeType to return false for the provided mime types.

Looks related to #20034

I created this PR with a proposed change: 1499


Affects: 5.0 RC3

Referenced from: commits bb327b9

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions