来自REST查询的UTF-8编码字符未正确呈现 [英] UTF-8 encoded characters from REST-query not rendered properly
问题描述
我正在使用提供所有内容为UTF-8编码的外部REST服务。
由于某些原因,我的应用程序无法正确处理响应。如果我转储回复,我会选择Lule?(应该是Luleå)。
编辑:
如果我转发(不更改)字符串到用户界面,例如:
flash.message =Test+ integrationService.testEncoding()
我所做的是在/ script文件夹中创建一个_Events.groovy文件,并在其中指定
eventConfigureTomcat = {tomcat - >
tomcat.connector.URIEncoding =UTF-8
tomcat.connector.useBodyEncodingForURI = true
}
在我的 Config.groovy
中也有以下内容:
grails.views.gsp.encoding =UTF-8
grails.converters.encoding =UTF-8
但这没有改变。答案仍然错误地显示。我不确定这是否是Grails的配置问题,以及嵌入式tomcat或其他内容。我目前正在Windows 7上运行我的测试设置,但同样的问题发生在我在Centos上运行的服务器上。请咨询。
编辑2:
如果我使用curl来使用REST服务,那么所有内容都会在输出中正确呈现。
EDIT3:
我使用 org.springframework.web.client.RestTemplate
和 HttpComponents
使用服务:
pre $ private static final HttpHeaders requestHeaders
static {
requestHeaders = new (HttpHeaders.ACCEPT,application / json)
requestHeaders.set(Accept- Encoding,gzip)
}
private final RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create()。build()))
...
...
public def testEncoding(){
ResponseEntity< String> response = restTemplate.exchange(
https://www.url.com,HttpMethod.GET,new HttpEntity< Object>(requestHeaders),
String.class)
def gamesJson = JSON.parse(response.getBody())
// ...
//解析gameJson的值
// ...
return testValue
}
Per StringHttpMessageConverter $ c $ / $ / $ / $ / c>到模板的消息转换器:
RestTemplate template = new RestTemplate();
template.getMessageConverters()
.add(0,new StringHttpMessageConverter(Charset.forName(UTF-8)));
ResponseEntity< Object> response = template.exchange(endpoint,method,entity,
Object.class);
I'm consuming an external REST service that provides all content as UTF-8 encoded.
For some reason my application cannot properly handle the response. If I dump the response I will se things like LuleÃ¥ (should be Luleå).
EDIT: The same behavior happens if i forward (without altering) the string to the UI, ex.:
flash.message = "Test" + integrationService.testEncoding()
What I did was to create a _Events.groovy file in the /script folder and specifying there that
eventConfigureTomcat = { tomcat ->
tomcat.connector.URIEncoding = "UTF-8"
tomcat.connector.useBodyEncodingForURI = true
}
I also have the following in my Config.groovy
:
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"
But that changed nothing. The response is still wrongly shown. I'm not sure if this is a configuration issue with Grails, with the embedded tomcat or with something else. I'm currently running my test setup on windows 7, but the same issue happens on my server running on Centos. Please advice.
EDIT2: If i consume the REST service using curl, everything is rendered correctly in the output.
EDIT3:
I'm using org.springframework.web.client.RestTemplate
and HttpComponents
to consume the service:
private static final HttpHeaders requestHeaders
static{
requestHeaders = new HttpHeaders()
requestHeaders.set(HttpHeaders.CONTENT_TYPE, "application/json")
requestHeaders.set(HttpHeaders.ACCEPT, "application/json")
requestHeaders.set("Accept-Encoding", "gzip")
}
private final static RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create().build()))
...
...
public def testEncoding(){
ResponseEntity<String> response = restTemplate.exchange(
"https://www.url.com", HttpMethod.GET, new HttpEntity<Object>(requestHeaders),
String.class)
def gamesJson = JSON.parse(response.getBody())
//...
//parse value from gamesJson
//...
return testValue
}
Per my previous answer:
You just need to add the StringHttpMessageConverter
to the template's message converters:
RestTemplate template = new RestTemplate();
template.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
ResponseEntity<Object> response = template.exchange(endpoint, method, entity,
Object.class);
这篇关于来自REST查询的UTF-8编码字符未正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!