2017-02-23 87 views
0

在我的REST API中,每当用户尝试访问不存在的资源时,我都会返回404。EntityResponse字符集编码错误

但是,Firefox不显示“404错误”页面,但抱怨字符编码。

这是我的控制器代码:

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

其中要求从服务实体此方法:

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

DetailedCountryJson是一个简单的JSON对象。所以,当我访问

localhost:8080/countries/1 

我得到有关这个国家的JSON,但是当我试图用一个id这是不是在数据库中,我得到

the error displayed by firefox

它说

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

+0

你能转换图像以英文文本? –

+0

@RickS我在译文中加入了翻译 – user2436719

回答

1

您可以使用response.setContentType("text/plain;charset=UTF-8"); 请参阅本link

+0

有没有什么办法可以用注释来做?我尝试在'@ RequestMapping'中添加'produce =“application/json; charset = UTF-8”',但是我得到了相同的结果 – user2436719

+1

好了,它现在在服务实体中起作用,我刚刚改变了'ResponseEntity.notFound() .header(“content-type”,“text/plain; charset = utf-8”)。build()'。谢谢 – user2436719