Я использую Java 1.7 и Spring 4.0.3 RELEASE
Существует внешний API, который закодирован в URL и выглядит примерно так:
curl -X GET \
https://api.myservice.com?query_id=1a4445b3e528996d9334b2b8670f786c&variables=%4B%20%0A%09%22property_id%22%63%20%2232144652-414F-4348-4953-455f32373030%22%2C%20%0A%09%22first%22%3A%201%2C%20%0A%09%22publishState%22%3A%20%22PUBLISHED%22%2C%0A%09%22orderByDirection%22%3A%20%22DESC%22%20%0B%7D' \
-H 'authorization: Bearer dcJhbGciOiJIUzI1NmJ8.eyJ1c2VybmFtZSI6bnVsbCwiZGV2aWNlSWQiOm51bGwsImNsaWVudElkIjoiODJhZUZCTEpJWnMxSG91d3djanVpYlRsUlVuSDlXWTciLCJhZElkIjpudWxsLCJleHAiOjE1Mzc1MjAyOTksImlhdCI6MTUzNzUxNjY5OX0.cdmLbuM6r6pfHiwvDxygVK9aMVx29BTH_AQ3Vyz1maQ
Может работать вмоя Java-программа выглядит так:
private static String encodedUrl = "https://api.myservice.com?query_id=1a4445b3e528996d9334b2b8670f786c&variables=%4B%20%0A%09%22property_id%22%63%20%2232144652-414F-4348-4953-455f32373030%22%2C%20%0A%09%22first%22%3A%201%2C%20%0A%09%22publishState%22%3A%20%22PUBLISHED%22%2C%0A%09%22orderByDirection%22%3A%20%22DESC%22%20%0B%7D";
public static String hitDatastoreWithEncodedUrl(String encodedUrl) {
String accessToken = OAuth2Client.generateAccessToken();
String response = null;
try {
URL url = new URL(ENCODED_URL);
HttpsURLConnection connection =(HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response code: " + connection.getResponseCode());
System.out.println("Response message: " + connection.getResponseMessage());
// Read the response:
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
logger.info("\n\n\t\tJSON Response: " + line + "\n\n");
response = line;
}
reader.close();
}
catch (MalformedURLException mue) {
logger.error("Problem occurred in ", mue);
}
catch (IOException ioe) {
logger.error("Problem occurred in ", ioe);
}
return response;
}
Однако я действительно хочу использовать RestTemplate, вот что я пробовал:
public static HttpEntity<String> hitDatastoreWithEncodedUrl(String encodedUrl) {
String accessToken = OAuth2Client.generateAccessToken();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + accessToken);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(encodedUrl)
.queryParam("query_id", "1a4445b3e528996d9334b2b8670f786c")
.queryParam("variables", "%4B%20%0A%09%22")
.queryParam("property_id", "22%63%20%2232144652-414F-4348-4953-455f32373030%22%2C%20%0A%09%22first%22%3A%201%2C%20%0A%09%22publishState%22%3A%20%22PUBLISHED%22%2C%0A%09%22orderByDirection%22%3A%20%22DESC%22%20%0B%7D");
HttpEntity<?> entity = new HttpEntity<>(headers);
HttpEntity<String> response = restTemplate.exchange(
builder.toString(),
HttpMethod.GET,
entity,
String.class);
return response;
}
Получите эту ошибку:
Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
Кажется, что query_id является единственной строкой запроса, тогда как: property_id, first, ключ_доступа, orderByDirection и DESC являются частью URL на конкатенацию?
Как мне структурировать этоURL с использованием Spring RestTemplate?