Untitled
unknown
plain_text
2 years ago
1.5 kB
7
Indexable
Certainly! Here's a simplified example using `RestTemplate` to make an API call in Java code with a Bearer token:
```java
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
public class ApiCaller {
public static void main(String[] args) {
// Replace these with your actual API endpoint and token
String apiUrl = "https://api.example.com/endpoint";
String bearerToken = "yourBearerToken";
// Set up headers with Bearer token
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(bearerToken);
// Set up RestTemplate
RestTemplate restTemplate = new RestTemplate();
// Make the API call
ResponseEntity<String> responseEntity = restTemplate.exchange(
apiUrl,
HttpMethod.GET, // Replace with your HTTP method
null,
String.class
);
// Access the response body
String responseBody = responseEntity.getBody();
System.out.println("API Response: " + responseBody);
}
}
```
Make sure to handle exceptions, customize the HTTP method, and adjust the code according to your specific requirements and Spring Boot version.Editor is loading...
Leave a Comment