1

In my REST API, I return a 404 whenever the user is trying to access a non existing resource.

However, Firefox does not display the "404 error" page, but complains about character encoding.

Here is my controller code:

@RequestMapping("/countries/{countryId}")
public ResponseEntity<?> country(@PathVariable Integer countryId) {
    return countriesService.getCountry(countryId);
}

which calls this method from the service entity:

public ResponseEntity<?> getCountry(Integer countryId) {
    Country country = countryDAO.findById(countryId);
    if (country == null)
        return ResponseEntity.notFound().build();
    return ResponseEntity.ok(new DetailedCountryJson(country));
}

DetailedCountryJson is a simple json object. So when I access

localhost:8080/countries/1

I get the json related to this country, but when I try with an id which is not in the database, I get

the error displayed by firefox

It says

The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

lolo
  • 706
  • 6
  • 19

1 Answers1

2

You can useresponse.setContentType("text/plain;charset=UTF-8"); refer this link

Community
  • 1
  • 1
Phạm Quốc Bảo
  • 864
  • 2
  • 10
  • 19
  • Is there any way I can do it with annotations? I tried to add `produces = "application/json;charset=UTF-8"` into the `@RequestMapping` but i get the same result – lolo Feb 23 '17 at 16:22
  • 2
    Ok it works now in the service entity, I just changed `ResponseEntity.notFound().header("content-type", "text/plain; charset=utf-8").build()`. Thanks – lolo Feb 23 '17 at 16:41