Home  >  Forum  >  Spring
Post New Query

Facing problem in calling get method API with query parameters using rest template.



Asked on December 04, 2017
Hi,

I am trying to call one get method API by passing query parameters using rest template. I am passing the request in the proper format only, but I am getting the
 error as message as "400 null". 

Following is the curl command which is working properly with POSTMAN:
curl -X GET \
  'http://localhost:8086/uaa/Groups?filter=identity_zone_id%20eq%20%22uaa%22' \
  -H 'authorization: Bearer eyJhbGciOiJIUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiJlMmEwYzgwN2M4ZDQ0YzhiYTE1ZmUwNGJmZjFmNzNlYyIsInN1YiI6IjBkZTZlZDhkLWU1OGMtNGExNC1iNTdmLWM5Mjk2Y2JlYjM3NSIsInNjb3BlIjpbImFwaXMucmVhZCIsIm9wZW5pZCIsImFwaXMud3JpdGUiLCJ1YWEuYWRtaW4iLCJzY2ltLndyaXRlIiwic2NpbS5yZWFkIiwidWFhLnVzZXIiXSwiY2xpZW50X2lkIjoiY2xpZW50IiwiY2lkIjoiY2xpZW50IiwiYXpwIjoiY2xpZW50IiwiZ3JhbnRfdHlwZSI6ImF1dGhvcml6YXRpb25fY29kZSIsInVzZXJfaWQiOiIwZGU2ZWQ4ZC1lNThjLTRhMTQtYjU3Zi1jOTI5NmNiZWIzNzUiLCJvcmlnaW4iOiJ1YWEiLCJ1c2VyX25hbWUiOiJtYXJpc3NhIiwiZW1haWwiOiJtYXJpc3NhQHRlc3Qub3JnIiwiYXV0aF90aW1lIjoxNTEyNDU1NzU4LCJyZXZfc2lnIjoiMWE5MzYzNWYiLCJpYXQiOjE1MTI0NTU3NTksImV4cCI6MTUxMjQ5ODk1OSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwL3VhYS9vYXV0aC90b2tlbiIsInppZCI6InVhYSIsImF1ZCI6WyJzY2ltIiwiYXBpcyIsInVhYSIsIm9wZW5pZCIsImNsaWVudCJdfQ.LbVu7inr5htyOHPyoN_KzIyFRZZiiiCV4xZHTCJZR30'


Same format only I am passing the request through rest template. Following is the code which I am trying:


public String listGroup(String accessToken){
String transactionUrl = "http://localhost:8086/uaa/Groups";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer "+accessToken);
   HttpEntity<String> entity = new HttpEntity<String>(headers);

UriComponentsBuilder builder = UriComponentsBuilder
    .fromUriString(transactionUrl)
    // Add query parameter
    .queryParam("filter", "identity_zone_id+eq+%22uaa%22");
    

RestTemplate restTemplate = new RestTemplate();
//String response = restTemplate.getForObject(builder.toUriString(), String.class);
System.out.println("URL===="+builder.toUriString());
try {
String param= URLDecoder.decode("filter=identity_zone_id%2Beq%2B%2522uaa%2522", "UTF-8");
            System.out.println("param=="+param+"\n");

String decodedURL = URLDecoder.decode(builder.toUriString(), "UTF-8");
            System.out.println("decodedURL=="+decodedURL+"\n");
            System.out.println("----------------------------------------------------");

            System.out.println(URLDecoder.decode(builder.toUriString(), "UTF-8"));
            System.out.println("entity=="+entity);

//String decodedURL = java.net.URLDecoder.decode(builder.toUriString(), "UTF-8");

ResponseEntity<String> response = 
        restTemplate.exchange(decodedURL, 
                              HttpMethod.GET, entity, String.class, param);
return "Success-"+response;
} catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
return "error";
}

Can anyone help me out in resolving this issue.

Thanks & Regards,

Shilpa Kulkarni



Replied on December 04, 2017
Try using buildAndExpand

String transactionUrl = "http://localhost:8086/uaa/Groups?filter={value}";
UriComponentsBuilder builder=  UriComponentsBuilder.fromUriString(transactionUrl).buildAndExpand("identity_zone_id+eq+%22uaa%22");



Replied on December 05, 2017
Thanks a lot for the quick response.This is worked.


Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us