我试图通过使用rest模板传递查询参数来调用一个get方法API。我只是以正确的格式传递请求,但我得到了 错误消息为“400 null”。
以下是与POSTMAN一起正常工作的curl命令:
curl -X GET \
'http://localhost:8086/uaa/Groups?filter=identity_zone_id%20eq%20%22uaa%22' \
-H 'authorization: Bearer eyJhbGciOiJIUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiJlMmEwYzgwN2M4ZDQ0YzhiYTE1ZmUwNGJmZjFmNzNlYyIsInN1YiI6IjBkZTZlZDhkLWU1OGMtNGExNC1iNTdmLWM5Mjk2Y2JlYjM3NSIsInNjb3BlIjpbImFwaXMucmVhZCIsIm9wZW5pZCIsImFwaXMud3JpdGUiLCJ1YWEuYWRtaW4iLCJzY2ltLndyaXRlIiwic2NpbS5yZWFkIiwidWFhLnVzZXIiXSwiY2xpZW50X2lkIjoiY2xpZW50IiwiY2lkIjoiY2xpZW50IiwiYXpwIjoiY2xpZW50IiwiZ3JhbnRfdHlwZSI6ImF1dGhvcml6YXRpb25fY29kZSIsInVzZXJfaWQiOiIwZGU2ZWQ4ZC1lNThjLTRhMTQtYjU3Zi1jOTI5NmNiZWIzNzUiLCJvcmlnaW4iOiJ1YWEiLCJ1c2VyX25hbWUiOiJtYXJpc3NhIiwiZW1haWwiOiJtYXJpc3NhQHRlc3Qub3JnIiwiYXV0aF90aW1lIjoxNTEyNDU1NzU4LCJyZXZfc2lnIjoiMWE5MzYzNWYiLCJpYXQiOjE1MTI0NTU3NTksImV4cCI6MTUxMjQ5ODk1OSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwL3VhYS9vYXV0aC90b2tlbiIsInppZCI6InVhYSIsImF1ZCI6WyJzY2ltIiwiYXBpcyIsInVhYSIsIm9wZW5pZCIsImNsaWVudCJdfQ.LbVu7inr5htyOHPyoN_KzIyFRZZiiiCV4xZHTCJZR30'
仅相同格式我通过rest模板传递请求。以下是我正在尝试的代码:
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";
}
任何人都可以帮我解决这个问题。
谢谢&amp;的问候,
Shilpa Kulkarni
答案 0 :(得分: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");