简体   繁体   English

从RestTemplate Spring Boot获取并解码响应

[英]Get and decode a response from RestTemplate spring boot

I am encoding a URL and when I get the response back it is encoded as well. 我正在编码一个URL,当我收到响应时,它也会被编码。 What I am having a issue with is decoding it. 我遇到的问题是对其进行解码。

String encoded = URLEncoder.encode(text,"UTF-8");

        UriComponentsBuilder builder = UriComponentsBuilder
            .fromUriString("https://google.com/translate")
            .queryParam("srcLang", srcLang)
            .queryParam("tgtLang", tgtLang)
            .queryParam("text", encoded);

        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),HttpMethod.GET, request, String.class);

what I was trying to do is: 我试图做的是:

String decodedResult = UriUtils.decode(response.toString(),"UTF-8");

But that didn't work. 但这没有用。

advice? 建议?

response object is of ResponseEntity type. 响应对象是ResponseEntity类型。 You should first get body from response then decode it. 您应该首先从响应中获取主体,然后对其进行解码。

String decodedResult = UriUtils.decode(response.getBody(),"UTF-8");

暂无
暂无

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

相关问题 Spring Boot RestTemplate 从回调中获取 Cookie - Spring Boot RestTemplate get Cookie from Callback REST API 响应为 XML 格式,使用 Resttemplate 从 Spring Boot 调用 - Rest API response as XML format, calling from Spring Boot with Resttemplate 没有响应类型的 Spring Boot RestTemplate 帖子 - Spring Boot RestTemplate post without response type 在spring boot resttemplate中将响应对象转换为实体 - Convert a response object to entity in spring boot resttemplate 当 Spring Boot 中的响应正文无效时,如何使用 RestTemplate 客户端进行 GET 请求? - How to use RestTemplate client for GET request when response body is void in Spring Boot? 带有参数的 RestTemplate GET 与 Spring 引导 Controller - RestTemplate GET with parameteres with Spring Boot Controller 如何在 Spring Boot 中使用 RestTemplate 从来自外部 REST API 的 JSON 响应中提取数据? - How to extract data from JSON response coming from external REST API using RestTemplate in Spring Boot? Spring Boot RestTemplate WebClient - 响应正文 JSON 空 - Spring Boot RestTemplate WebClient - Response Body JSON Empty 如何在Spring Boot中将RestTemplate的getBody数组列表响应映射到类中? - How to map getBody array list response of RestTemplate into class in Spring boot? Spring Boot RestTemplate ClientHttpRequestInterceptor 记录异常情况下的响应体 - Spring Boot RestTemplate ClientHttpRequestInterceptor log response body in case of exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM