Skip to content

RestController returning JSON String producing incorrect Content-Type charset #24813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
OmarHawk opened this issue Mar 30, 2020 · 1 comment
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue

Comments

@OmarHawk
Copy link

OmarHawk commented Mar 30, 2020

Affects: 5.2.2.RELEASE
(via spring-boot-starter 2.2.2.RELEASE - but would also affect the "master")

Hi,

I do have a @RestController annotated class which looks like this (removed any unrelevant things):

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/cart")
public class CartApiController {
    @GetMapping(path = "/get", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCart() {
        return "{success: true}";
    }
}

that method getCart() is supposed to return "application/json" content type. If I check that within a browser, I find the follwing response header:

2020-03-30_10h06_50

application/json;charset=ISO-8859-1

which is not right to my understanding.

I think, I have tracked it down to the following class:

https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

Where on line 49 it states

public static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;

which causes this issue.

Our workaorund at the moment is to use the following annotation config insteadwhich enforces UTF-8:
@GetMapping(path = "/get", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

but that makes use of a deprecated (due to #22788) field , which we don't want to do obviously.

Is this supposed to be like that and if not can you maybe fix this?

Thanks and best regards.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 30, 2020
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Mar 30, 2020
@rstoyanchev
Copy link
Contributor

This is a duplicate of #24123.

@rstoyanchev rstoyanchev added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 30, 2020
@rstoyanchev rstoyanchev self-assigned this Mar 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants