简体   繁体   English

EntityResponse字符集编码错误

[英]EntityResponse charset encoding error

In my REST API, I return a 404 whenever the user is trying to access a non existing resource. 在我的REST API中,每当用户尝试访问不存在的资源时,我都会返回404。

However, Firefox does not display the "404 error" page, but complains about character encoding. 但是,Firefox不会显示“ 404错误”页面,但是会抱怨字符编码。

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. DetailedCountryJson是一个简单的json对象。 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 我得到了与此国家有关的json,但是当我尝试使用不在数据库中的id时,我得到了

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. 如果文档包含来自US-ASCII范围之外的字符,则在某些浏览器配置中,文档将呈现乱码。 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. 需要在传输协议中声明文件的字符编码,或者文件需要使用字节顺序标记作为编码签名。

You can use response.setContentType("text/plain;charset=UTF-8"); 您可以使用response.setContentType("text/plain;charset=UTF-8"); refer this link 参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM