8

i got some problems with my SpringBoot REST Controller. This simply does a http GET call to our database and should return a simple String / json. when i call the URL simply in my browser or via my angular 3 app, the response has some charset errors and i don't know, how to fix them. I suggest, it is a UTF-8 problem.

First to show you the output: this is how it comes from the Controller: MeinekestraÃe and it should be Meinekestraße

here is a part of my SpringBoot Controller:

@Controller
public class RecieverController {

@Value("${server}")
private String server;

@Value("${user.token}")
private String token;   

@RequestMapping(value="/reciever", method=RequestMethod.GET, produces = "text/plain;charset=UTF-8")
@ResponseBody
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
public String getRecieverData(
        @RequestHeader(value="Accept") String accept,
        @RequestHeader(value="Host") String host) {

    final String url = server + "/rest/client/profile";

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    headers.set("Auth-Token", token); // user_token

    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);

    return response.getBody();
}}

I tried the following things, but nothing changed in the output.

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

or this

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
    .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

any other ideas what could be the problem? the database isnt the issue. Everything is stored correctly there.

Edit: this is the screenshot of the header from the output enter image description here and a part from the json output: enter image description here

The Problem could be solved by adding both

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

and this

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters()
    .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

Thanks @dienerd for helping me via chat

Peter
  • 267
  • 2
  • 5
  • 14
  • please include the errors/result of the web browser access. is there any reasons that you are not using the consumes setting on @RequestMapping? –  Jan 15 '18 at 16:27
  • @Peter have you tried "{ 'text': 'cadcda' } ".getBytes("UTF-8"); – mohit sharma Jan 15 '18 at 16:27
  • @dienerd the result is a valid json just the coding are like in the example above. Or what you mean with that? Could You please be more specific, so I can provide the information. – Peter Jan 16 '18 at 06:23
  • @mohit sharma how in your opinion should I use this? I can't just do return response.getBody().getBytes("UTF-8"); this gives an error. – Peter Jan 16 '18 at 06:26
  • @Peter have you tried directly logging the response to output to see what that looks like? like the raw string? just to console or some such? Just FYI: if you want to consume JSON only, it doesn't hurt to put that into the mix that instead of: @RequestMapping(value="/reciever", method=RequestMethod.GET, produces = "text/plain;charset=UTF-8") you might add a: consumes = MediaType.APPLICATION_JSON into it. –  Jan 16 '18 at 09:53
  • @dienerd i tried to use the add consume like this: @RequestMapping(value="/reciever/default_address", method=RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) but it doesnt change the output. I also do a System.out.println(response.getBody()); there is also the error in the console – Peter Jan 16 '18 at 10:19
  • @Peter ... here's another tip that has nothing to do with solving your problem but just FYI: If you use the annotation RestController instead of Controller... RestController combines Controller & ResponseBody. So you can omit ResponseBody from your service method annotations. It's an annotation 2-for-1 sale situation –  Jan 16 '18 at 10:21
  • @Peter: i didn't think adding consumes into the mix would do anything, it's just better to have that constraint there so it errors right away if you don't get valid JSON. here's a silly question. so you do straight up test from POSTMAN & it comes out Meinekestraße via POSTMAN, too? at this point, we have to eliminate everything. –  Jan 16 '18 at 10:37
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163239/discussion-between-dienerd-and-peter). –  Jan 16 '18 at 10:46
  • @dienerd thanks for the hint with RestController. I changed it right away. now to your other question. Yes, i tried Postman and the result is correct. The streetname is how it should be "Meinekestraße". it has something todo with recieving the data from the database and displaying them. i also tried a direct connection from my angular frontend to the database. the output is also correct. but we need the sprinbgboot backent between frontend and database – Peter Jan 16 '18 at 10:50
  • Where did you add spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true? – mLamaa Mar 10 '21 at 10:24

1 Answers1

16

For the ones looking for a way to force encoding request/response in @RestController in a Spring Boot project.

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

is deprecated, in your application.yaml or application.properties use the following:

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true

This did the trick for me.

Yuri Andrade
  • 161
  • 1
  • 5
  • Hello dear, can you give some details, I'm totally new to spring boot and I have encoding problem on post requests – mLamaa Mar 10 '21 at 10:13
  • You should place this configuration in your application.yaml or application.properties, this will configure the spring boot to force that encoding in the servlet – Yuri Andrade Mar 19 '21 at 15:44