Skip to content

Instantly share code, notes, and snippets.

@marsyang1
Last active March 14, 2016 05:21
Show Gist options
  • Save marsyang1/ed17ad9a3dad3d6983e6 to your computer and use it in GitHub Desktop.
Save marsyang1/ed17ad9a3dad3d6983e6 to your computer and use it in GitHub Desktop.
Java unicode to char
/**
* http://stackoverflow.com/questions/11145681/how-to-convert-a-string-with-unicode-encoding-to-a-string-of-letters
* use StringEscapeUtils.unescapeJava(encodeUniCode String)
*/
@Component
public class RestClient {
private final RestTemplate restTemplate;
public RestClient(){
restTemplate = new RestTemplate();
HttpHeaders headers = getJsonHeader();
}
public HttpHeaders getJsonHeader(){
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
return headers;
}
public String exchangeToString(String url, HttpMethod method) {
return exchangeToString(url,method,getJsonHeader());
}
public String exchangeToString(String url, HttpMethod method , HttpHeaders headers) {
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response =
restTemplate.exchange(url, method, requestEntity, String.class);
String body = response.getBody();
return StringEscapeUtils.unescapeJava(body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment