Skip to content

Spring escapes Emoji's on json-marshalling #29819

Closed as not planned
Closed as not planned
@Podbrushkin

Description

@Podbrushkin

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

  1. 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
  2. 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);
	}
}
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectNeeds a fix in external projectin: webIssues in web modules (web, webmvc, webflux, websocket)status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions