Home RestTemplate is unable to fetch response with # charecter
Reply: 0

RestTemplate is unable to fetch response with # charecter

Akshay Singh
1#
Akshay Singh Published in 2018-02-13 19:09:08Z

Context: I am using RestTemplate as my REST client to fetch a response which might contain special characters like hash (#)

Issue: I'm fetching a JSON response like this-

JSONObject response = new JSONObject(new RestTemplate().getForObject(url, String.class));

and I am expecting a response like :

{
  "_embedded" : {
    "property" : [ {
      "attributeWithHash" : "valueContains#Hash"
    }]
},
  "_links" : {
    "self" : {
      "href" : "http://...."
    }
}

but instead I'm getting :

{   "_embedded" : {
    "property" : []
},
"_links" : {
    "self" : {
      "href" : "http://...."
    } }

The above code works as expected if my response does not contain # charecter.

Already tried with no success:

I thought my response header isn't set right, so I tried to add a custom header to include UTF8 encoding like this-

RestTemplate template = new RestTemplate();
headers.setAccept(Arrays.asList(new MediaType("application", "json" , Charset.forName("utf-8"))));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
JSONObject response = new JSONObject(template.exchange(url, HttpMethod.GET, entity, String.class));
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338618 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO