首页 文章

获取400 null作为错误消息,使用rest模板调用带有查询参数的get方法API

提问于
浏览
0

我试图通过使用rest模板传递查询参数来调用一个get方法API . 我只是以正确的格式传递请求,但我收到错误消息为“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";
}

任何人都可以帮我解决这个问题 .

感谢和问候,

Shilpa Kulkarni

1 回答

  • 0

    而不是提到url中的端口使用URIComponentsBuilder传递它

    String transactionUrl = "http://localhost/uaa/Groups";
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromUriString(transactionUrl)
            .port(8086)
            // Add query parameter
            .queryParam("filter", "identity_zone_id+eq+%22uaa%22");
    

相关问题