0

I have multiple REST endpoints that return JSON objects. For most locales the responses are correct (all symbols are correctly shown in the response and the Content-Type is application/json).

If I provide the Accept-Language header with value "en_NL", the Content-Type header changes to "application/json;charset=ISO-8859-1. The response body also contains incorrect encoded symbols. (eg. € turns into €).

I'm not sure where the content-type header changes and I can't find any trace in any filter/config or jaxb.

For (un)marshalling I use jaxb. The application is build using Spring 4.1.7 and it is hosted on a tomcat server.

Controller example:

    @RequestMapping(value = "/by-bundle", method = RequestMethod.GET)
    @ResponseBody
    public SbMessageMap getByBundleCodes(final @RequestParam("groups") String[] groups) {
        // return json object here
    }

Thanks in advance!

Cornel Janssen
  • 681
  • 1
  • 11
  • 33
  • I don't know spring that well but instead of tracking down how the encoding is determined you maybe could just set it to a fixed value like UTF-8. – Thomas Dec 18 '18 at 17:39
  • I can add the content type in a filter, but that would be kinda a dirty solution. – Cornel Janssen Dec 18 '18 at 17:53
  • 1
    Well, I'd say that if the service produces UTF-8 encoded messages then it would actually be a cleaner solution to explicitly state so. – Thomas Dec 19 '18 at 08:35

1 Answers1

2

Did you try setting the charset with the produces parameter at @RequestMapping?

@RequestMapping(value = "/by-bundle", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
  • I would prefer to not adjust all the endpoints that are currently available in the project. Thanks for an answer though! – Cornel Janssen Dec 18 '18 at 17:53
  • 1
    @CornelJanssen You can add the charset to a bean or config instead so you don't have to modify all the endpoints. See https://stackoverflow.com/a/9394539/4614788, https://stackoverflow.com/a/51474450/4614788 and https://stackoverflow.com/a/39913506/4614788 – Glenn Van Schil Dec 19 '18 at 09:03