Skip to content

Expose Jackson2CodecSupport mime types for custom override [SPR-15474] #20034

Closed
@spring-projects-issues

Description

@spring-projects-issues

Daniel Lavoie opened SPR-15474 and commented

Some web services offers JSON content without properly typing the content type. For example, a bad api serving json content but setting the content-type to text/plain. The brand new shiny WebClient relies on the Jackson2CodecSupport to decode the stream. Saldy, there is now way to manually define the supported mime types.

The class Jackson2CodecSupport does not offers any means to override the supported mime types.

I can provide a PR to expose an extension mechanism.

Jackson2JsonDecoder

@Override
public boolean canDecode(ResolvableType elementType, MimeType mimeType) {
  JavaType javaType = this.objectMapper.getTypeFactory().constructType(elementType.getType());
  // Skip String: CharSequenceDecoder + "*/*" comes after
  return (!CharSequence.class.isAssignableFrom(elementType.resolve(Object.class)) &&
    this.objectMapper.canDeserialize(javaType) && supportsMimeType(mimeType));
}

Jackson2CodecSupport

  protected static final List<MimeType> JSON_MIME_TYPES = Arrays.asList(
    new MimeType("application", "json", StandardCharsets.UTF_8),
    new MimeType("application", "*+json", StandardCharsets.UTF_8));


  protected boolean supportsMimeType(MimeType mimeType) {
    return (mimeType == null ||
      JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
  }

Affects: 5.0 M1

Referenced from: commits 8231812

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions