Skip to content

StringHttpMessageConverter addDefaultHeaders() should check media type for wildcard before setting it into headers #24283

@artembilan

Description

@artembilan

I have just started to fail after this change 9b30d46.

My content type candidate is like this one:

if (CollectionUtils.isEmpty(acceptTypes)) {
		acceptTypes = Collections.singletonList(MediaType.ALL);
}

So, MediaType.ALL is really compatible with MediaType.APPLICATION_JSON, but it is wrong value for the Content-Type header:

java.lang.IllegalArgumentException: Content-Type cannot contain wildcard type '*'

	at org.springframework.util.Assert.isTrue(Assert.java:118)
	at org.springframework.http.HttpHeaders.setContentType(HttpHeaders.java:949)
	at org.springframework.http.converter.StringHttpMessageConverter.addDefaultHeaders(StringHttpMessageConverter.java:109)
	at org.springframework.http.converter.StringHttpMessageConverter.addDefaultHeaders(StringHttpMessageConverter.java:44)
	at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:211)

Any clues what to use instead of MediaType.ALL when no Accept header in the request?

Or maybe this fix should be improved to skip MediaType.ALL as it is done in the super class:

if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
		contentTypeToUse = getDefaultContentType(t);
}

Note that AbstractMessageConverterMethodProcessor is not used in Spring Integration.
There logic in the HttpRequestHandlingMessagingGateway is like this: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGateway.java#L171

So, instead of falling back to the MediaType.APPLICATION_OCTET_STREAM we try to rely on the Content-Type populated by the converted which fits to the payload we would like to return into the response.

Originally based on discussion.

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