I've been working with Spring Boot 2.0.0.RC1
and use spring-boot-starter-webflux
in order to build a REST Controller that returns a flux of text data.
@GetMapping(value = "/")
public Flux<String> getData(){
return Flux.interval(Duration.ofSeconds(2))
.map(l -> "Some text with umlauts (e.g. ä, ö, ü)...");
}
Since the text data contains some umlauts (e.g. ä, ö, ü), I would like to change the Content-Type header of the response from text/event-stream
to text/event-stream;charset=UTF-8
. Therefore, I tried wrapping to flux into a ResponseEntity
. Like this:
@GetMapping(value = "/")
public ResponseEntity<Flux<String>> getData(){
return ResponseEntity
.ok()
.contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
.body(Flux.interval(Duration.ofSeconds(2))
.map(l -> "Some text with umlauts (e.g. ä, ö, ü)..."));
}
Now, making a curl request to the endpoint shows that the Content-Type remains the same:
< HTTP/1.1 200 OK
< transfer-encoding: chunked
< Content-Type: text/event-stream
<
data:Some text with umlauts (e.g. ├ñ, ├Â, ├╝)...
I suspected the MediaType.parseMediaType()
method to be the issue, but the media type is parsed correctly (as this screenshot shows):
However, the parameter charset
seems to be ignored. How can I change the encoding to UTF-8 so that the browser interprets the umlaut characters correctly?
EDIT: Setting within the GetMapping
annotation the produces
field does not work either.
@GetMapping(value = "/", produces = "text/event-stream;charset=UTF-8")
public ResponseEntity<Flux<String>> getData(){
return ResponseEntity
.accepted()
.contentType(MediaType.parseMediaType("text/event-stream;charset=UTF-8"))
.body(Flux.interval(Duration.ofSeconds(2))
.map(l -> "Some text with umlauts (e.g. ä, ö, ü)..."));
}