简体   繁体   English

RestTemplate编码问题

[英]RestTemplate encoding issue

While getting data from one of web service quote(") is coming as (?) when i use Rest Template. I tested the web service in the postman on chrome and it giving correct characters. I tried encoding UTF-8, but no success. 当我使用Rest模板时,从其中一个Web服务引用(“)获取数据时会显示(?)。我在chrome上测试了邮件中的Web服务并给出了正确的字符。我尝试编码UTF-8,但没有成功。

I checked following are encoding from web service provider : 我检查了以下是来自Web服务提供商的编码:

Cache-Control → private Connection → close Content-Encoding → gzip Content-Length → 3407 Content-Type → text/xml; 缓存控制→专用连接→关闭内容编码→gzip内容长度→3407内容类型→文本/ xml; charset=ISO-8859-1 Date → Wed, 10 Jun 2015 13:35:53 GMT Server → Google Search Appliance Vary → Accept-Encoding X-Frame-Options → SAMEORIGIN x-content-type-options → nosniff x-xss-protection → 1; charset = ISO-8859-1日期→星期三,2015年6月10日13:35:53 GMT服务器→Google Search Appliance Vary→Accept-Encoding X-Frame-Options→SAMEORIGIN x-content-type-options→nosniff x-xss-保护→1; mode=block 模式=块

Here is my code : 这是我的代码:

RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    MediaType mediaType = new MediaType("text", "xml", Charset.forName("ISO-8859-1"));

    headers.set("Accept", "text/xml; charset=ISO-8859-1");
    headers.setContentType(mediaType);
    headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
    headers.setAccept(Arrays.asList(mediaType));

    ResponseEntity<String> res = restTemplate.exchange(gsaSearchUrl, HttpMethod.GET, new HttpEntity<String>(headers), String.class);


    System.out.println(res.getBody());

You need to add a HttpMessageConverter for UTF-8 encoded Strings to your RestTemplate. 您需要为您的RestTemplate添加一个用于UTF-8编码的字符串的HttpMessageConverter。 Something like this will do it for you: 像这样的东西会为你做的:

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

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

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