Skip to content

Use preset Content-Type for streaming and reactive responses in Spring MVC #35130

@rstoyanchev

Description

@rstoyanchev

We have supported a preset "Content-Type" for regular responses for a long time with #20720, but we do not do the same for streaming and reactive type return values.

For example with this:

@PostMapping(path = "/test")
public ResponseEntity<Publisher<?>> either(@RequestBody TypeInput input) {
	if ("text".equalsIgnoreCase(input.getType())) {
		return ResponseEntity.ok()
				.contentType(MediaType.TEXT_EVENT_STREAM)
				.body(Flux.interval(Duration.ofSeconds(1))
						.map(i -> "Stream response - " + LocalTime.now() + " (ID: " + i + ")"));
	}
	return ResponseEntity.ok()
			.contentType(MediaType.APPLICATION_JSON)
			.body(Mono.just(new Thing("This is a JSON response")));
}

A request with a general Accept header */* doesn't work for the Flux case since we can't determine the streaming format to determine.

For 7.0 we can take the preset Content-Type as-is, i.e. bypassing content negotiation.

For 6.2, we could do a bit less, taking the preset Content-Type if content negotiation doesn't produce anything specific in to avoid potential surprises from side effects where the Content-Type might be preset (in error), but is currently ignored.

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