Spring Restemplate response with %20 instaed spaces

时间:2019-01-09 22:18:37

标签: java spring resttemplate

I'm tried to use Restemplate to make a simple GET call, the response give me a String text that contains some spaces, the problem is that instead spaces, it give me %20. Can anyone help me please?

This is my code:

    RestTemplate restTemplate = new RestTemplate();
    String profanityUrl = "https://www.purgomalum.com/service/json";
    UriComponentsBuilder builder = UriComponentsBuilder .fromUriString(profanityUrl).queryParam("text", "some text");
    ResponseDTO response = restTemplate.getForObject(builder.toUriString(), ResponseDTO.class);

The ResponseDTO is :

public class ResponseDTO {

  private String result;
  public String getResult() {
    return result;
  }
  public void setResult(String result) {
     this.result = result;
  }
}

And the result atribute gives me: 'some%20text' instead of 'some text' (with the space)

1 个答案:

答案 0 :(得分:0)

A quick fix is to just decode the result value

private String decodeResponse(String response) {
    try {
        return URLDecoder.decode(response, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}