Closed as not planned
Description
Affects: Spring 6.0.3
Emojis are converted to Unicode escape sequences
When returning objects out of @RestController
, all UTF-8 characters from the object's fields are interpreted correctly, but emojis are devoured from "👾" to "\uD83D\uDC7E".
Minimal Reproducible Example
- Create Spring Boot project:
curl -G https://start.spring.io/starter.zip -d dependencies=web -d javaVersion=11 -d type=maven-project -o demo.zip
- Create
.\src\main\java\com\example\demo\rest\MainRest.java
:
package com.example.demo.rest;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
@RestController
public class MainRest {
@GetMapping("/greeting0")
Map<String,String> greet() {
var map = new HashMap<String,String>();
map.put("content","Οὐχὶ ταὐτὰ παρίσταταί 0 👾");
return map;
}
@GetMapping("/greeting1")
String greet(ObjectMapper objectMapper) throws Exception {
var map = new HashMap<String,String>();
map.put("content","Οὐχὶ ταὐτὰ παρίσταταί 1 👾");
return objectMapper.writeValueAsString(map);
}
}
- Run application and call these endpoints:
curl "http://localhost:8080/greeting0"
curl "http://localhost:8080/greeting1"
Expected output:
{"content":"Οὐχὶ ταὐτὰ παρίσταταί 0 👾"}
{"content":"Οὐχὶ ταὐτὰ παρίσταταί 1 👾"}
Actual output:
{"content":"Οὐχὶ ταὐτὰ παρίσταταί 0 \uD83D\uDC7E"}
{"content":"Οὐχὶ ταὐτὰ παρίσταταί 1 👾"}
This is Spring Boot, but same behaviour can be reproduced in bare Spring MVC with this setting:
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="jacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
and with produces="text/plain;charset=UTF-8"
in @GetMapping("/greeting1")
.
Jackson-databind works fine not only in the example above (when called directly), but in standalone application with no other dependencies as well. So I assume it's something with Spring MVC, not with Jackson or Boot.