Im new to Spring and im trying to understand it. Is it possible for me to use Spring boot to make calls to someone else's api? for example, the api im using needs to be authenticated using credentials they give me. I would like to use their API to make POST requests. So far I have found documents that relate to using your own API. These requests require headers to be passed as well.
Whats the simplest way I can do a POST request, passing my credentials and the required headers using Spring ?
Any help would be appreciated.
if i understand u well, u need to post another web-api with credentials okay you can use
restTemplate like the following example
public List<EtisAccount> getAllActiveAccount(){
logger.debug("Debug: in Class \t"+this.getClass().getName()+" Method Name is: \t"+new Object() {}.getClass().getEnclosingMethod().getName());
Properties sprinklrProp=sprinklrProperties.getSprinklrKeys();
SprinklrCredential sprinklrCredential=credentialBuilder.getSprinklrCredential();
RestTemplate restTemplate= new RestTemplate();
HttpHeaders header = new HttpHeaders();
header.setBearerAuth(sprinklrCredential.getAccess_token());
header.add("key", sprinklrCredential.getApi_key());
header.set("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);
UriComponentsBuilder uriBuilder= UriComponentsBuilder.fromUriString(sprinklrProp.getProperty("sprinlrUri").toString())
.queryParam("types", sprinklrProp.getProperty("accountTypes").toString());
HttpEntity<String> entity= new HttpEntity<>(header);
sslCertificateValidation.disable();
ResponseEntity<String> sprinklrResponse=restTemplate.exchange(uriBuilder.toUriString(),HttpMethod.GET,entity, String.class);
List<EtisAccount> activeAccouts=etisAccountHelper.getAllSocialEtisAccounts(sprinklrResponse.getBody());
logger.debug(String.valueOf(sprinklrResponse.getStatusCodeValue()));
logger.debug(activeAccouts.toString());
return activeAccouts;
}
this is sample example for call online-API using RestTemplate
here i build header with credentials (bearer authentication)
HttpHeaders header = new HttpHeaders();
header.setBearerAuth(sprinklrCredential.getAccess_token());
header.add("key", sprinklrCredential.getApi_key());
header.set("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);
and here i add header requestEntity to add to resttemplate request
HttpEntity<String> entity= new HttpEntity<>(header);
and here i get response using exchange method
ResponseEntity<String> sprinklrResponse=restTemplate.exchange(uriBuilder.toUriString(),HttpMethod.POST,entity, String.class);
You need following things :
Create a Spring web starter project from https://start.spring.io/
Create a new Java Class in your project and name it Controller. Add #RestController annotation to on class level.
Configure RestTemplate object in SpringBootApplication
Autowire that RestTemplate object in Controller class
Create a method in Controller class which using RestTemplate, will call the #PostMapping to their API.
You can try this way
This is the simple way to this
You can set user name and password as basic Auth
public class RESTInvoker {
private final String baseUrl;
private final String username;
private final String password;
public RESTInvoker(String baseUrl, String username, String password) {
this.baseUrl = baseUrl;
this.username = username;
this.password = password;
}
String getDataFromServer(String path) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(baseUrl + path);
URLConnection urlConnection = setUsernamePassword(url);
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
return sb.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private URLConnection setUsernamePassword(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
String authString = username + ":" + password;
String authStringEnc = new String(Base64.encodeBase64(authString.getBytes()));
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
return urlConnection;
}
}
Related
I have a yml file where I am storing my variables. I am accessing these variables in my program by using #Value annotation. The problem comes when I am writing mock test cases for such methods, I get null pointer exception in my test method. I'm not sure where I am going wrong.
I am making use of #TestPropertySource right now. I need the correct way of doing this.
Here is what I have tried so far.
My yml file looks like this, with many properties in it:
car:
services:
clientId: abcde
authTokenUrl: ....
.....
public Class CarExecution(){
#Value("${car.services.clientId}")
private String clientId;
#Value("${car.services.authTokenUrl}")
private String authTokenUrl;
public String getAccessToken() {
HttpHeaders headers = new HttpHeaders();
headers.set(CONTENT_TYPE, "application/x-www-form-urlencoded");
headers.set(ACCEPT, APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>("grant_type=password" + "&client_id=" + clientId ,headers);
ResponseEntity<Access> response = restTemplate.exchange(authTokenUrl, HttpMethod.POST, entity,A.class);
return response.getBody().token_type + " " +
response.getBody().access_token;
}
}
#RunWith(MockitoJUnitRunner.class)
#TestPropertySource(properties = {
"car.services.clientId = clientId ","car.services.authTokenUrl = authTokenUrl",
})
public class CarTest {
#Value("${car.services.clientId}")
private String clientId;
#Value("${car.services.authTokenUrl}")
private String authTokenUrl;
mockServer = MockRestServiceServer.createServer(restTemplate);
HttpHeaders headers = new HttpHeaders();
headers.set(CONTENT_TYPE, "application/x-www-form-urlencoded");
headers.set(ACCEPT, APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(
"grant_type=password&client_id=null", headers);
authTokenUrl = "";
new ResponseEntity<>("", HttpStatus.OK);
A access = new A();
access.access_token = "token";
access.token_type = "type";
response = new ResponseEntity<>(access, HttpStatus.OK);
Mockito.when(restTemplate.exchange(authTokenUrl, HttpMethod.POST, entity, A.class))
.thenReturn(response);
The problem might be with your Runner class as MockitoJUnitRunner doesn't initialise any beans or #Value annotations.
Spring's answer to it is SpringJUnit4ClassRunner which does these bits for you (documentation here).
Try replacing #RunWith(MockitoJUnitRunner.class) with #RunWith(SpringJUnit4ClassRunner.class).
I'm currently developing my first java program who'll make a call to a rest api(jira rest api, to be more especific).
So, if i go to my browser and type the url =
"http://my-jira-domain/rest/api/latest/search?jql=assignee=currentuser()&fields=worklog"
I get a response(json) with all the worklogs of the current user.
But my problem is, how i do my java program to do this ?
Like,connect to this url, get the response and store it in a object ?
I use spring, with someone know how to this with it.
Thx in advance guys.
Im adding, my code here:
RestTemplate restTemplate = new RestTemplate();
String url;
url = http://my-jira-domain/rest/api/latest/search/jql=assignee=currentuser()&fields=worklog
jiraResponse = restTemplate.getForObject(url,JiraWorklogResponse.class);
JiraWorkLogResponse is a simple class with some attributes only.
Edit,
My entire class:
#Controller
#RequestMapping("/jira/worklogs")
public class JiraWorkLog {
private static final Logger logger = Logger.getLogger(JiraWorkLog.class.getName() );
#RequestMapping(path = "/get", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity getWorkLog() {
RestTemplate restTemplate = new RestTemplate();
String url;
JiraProperties jiraProperties = null;
url = "http://my-jira-domain/rest/api/latest/search?jql=assignee=currentuser()&fields=worklog";
ResponseEntity<JiraWorklogResponse> jiraResponse;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders = this.createHeaders();
try {
jiraResponse = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(httpHeaders),JiraWorklogResponse.class);
}catch (Exception e){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
return ResponseEntity.status(HttpStatus.OK).body(jiraResponse);
}
private HttpHeaders createHeaders(){
HttpHeaders headers = new HttpHeaders(){
{
set("Authorization", "Basic something");
}
};
return headers;
}
This code is returning :
org.springframework.http.converter.HttpMessageNotWritableException
Anyone knows why ?
All you need is http client. It could be for example RestTemplate (related to spring, easy client) or more advanced and a little more readable for me Retrofit (or your favorite client).
With this client you can execute requests like this to obtain JSON:
RestTemplate coolRestTemplate = new RestTemplate();
String url = "http://host/user/";
ResponseEntity<String> response
= restTemplate.getForEntity(userResourceUrl + "/userId", String.class);
Generally recommened way to map beetwen JSON and objects/collections in Java is Jackson/Gson libraries. Instead them for quickly check you can:
Define POJO object:
public class User implements Serializable {
private String name;
private String surname;
// standard getters and setters
}
Use getForObject() method of RestTemplate.
User user = restTemplate.getForObject(userResourceUrl + "/userId", User.class);
To get basic knowledge about working with RestTemplate and Jackson , I recommend you, really great articles from baeldung:
http://www.baeldung.com/rest-template
http://www.baeldung.com/jackson-object-mapper-tutorial
Since you are using Spring you can take a look at RestTemplate of spring-web project.
A simple rest call using the RestTemplate can be:
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "http://localhost:8080/spring-rest/foos";
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
The issue could be because of the serialization. Define a proper Model with fields coming to the response. That should solve your problem.
May not be a better option for a newbie, but I felt spring-cloud-feign has helped me to keep the code clean.
Basically, you will be having an interface for invoking the JIRA api.
#FeignClient("http://my-jira-domain/")
public interface JiraClient {
#RequestMapping(value = "rest/api/latest/search?jql=assignee=currentuser()&fields=", method = GET)
JiraWorklogResponse search();
}
And in your controller, you just have to inject the JiraClient and invoke the method
jiraClient.search();
And it also provides easy way to pass the headers.
i'm back and with a solution (:
#Controller
#RequestMapping("/jira/worklogs")
public class JiraWorkLog {
private static final Logger logger = Logger.getLogger(JiraWorkLog.class.getName() );
#RequestMapping(path = "/get", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<JiraWorklogIssue> getWorkLog(#RequestParam(name = "username") String username) {
String theUrl = "http://my-jira-domain/rest/api/latest/search?jql=assignee="+username+"&fields=worklog";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<JiraWorklogIssue> response = null;
try {
HttpHeaders headers = createHttpHeaders();
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
response = restTemplate.exchange(theUrl, HttpMethod.GET, entity, JiraWorklogIssue.class);
System.out.println("Result - status ("+ response.getStatusCode() + ") has body: " + response.hasBody());
}
catch (Exception eek) {
System.out.println("** Exception: "+ eek.getMessage());
}
return response;
}
private HttpHeaders createHttpHeaders()
{
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic encoded64 username:password");
return headers;
}
}
The code above works, but can someone explain to me these two lines ?
HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
response = restTemplate.exchange(theUrl, HttpMethod.GET, entity, JiraWorklogIssue.class);
And, this is a good code ?
thx (:
I'm using Spring Boot to login to an external program using its basic authentication. That authentication exists of giving username + password and use Base64 to encode the header. After this I can use a call + header (containing password and username) to retrieve data.
Is there a simple way in Spring Boot to temporary save that header? And after the user is done, he/she can simply remove that header?
Otherwise the user has to keep giving username+password for every call to the API.
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
String url = "url";
RestTemplate template = new RestTemplate();
HttpHeaders headers = createHeaders("mail", "password");
ResponseEntity<JsonSearchResponse> response = template.exchange(url, HttpMethod.GET,
new HttpEntity<JsonSearchResponse>(headers), JsonSearchResponse.class);
JsonSearchResponse obj = response.getBody();
SpringApplication.run(DemoApplication.class, args);
}
public static HttpHeaders createHeaders(String username, String password) {
return new HttpHeaders() {{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
}};
}
}
In the end I'm going to put this code in a different class. But just prototyping at the moment.
The application is going to be a web application. Using a database. I'm also going to use Thymeleaf.
All users use the same backend. As it is usually the case with web applications.
I am trying to call a rest api and get the data from the api. I need to add dynamic parameters to the url in spring boot. I am a bit lost as how should I go about it. Can anyone kindly suggest me something?
RestTemplate restTemplate = new RestTemplate();
String consumeJSONString = restTemplate.getForObject("https://maps.googleapis.com/maps/api/geocode/json?latlng=5.47686,-73.961452&key=YOUR_API_KEY"
, String.class);
I would like to append latlng and api key in the url dynamically. I would really appreciate any suggestions.
You have to use the following variation of getForObject method
restTemplate.getForObject(url, responseType, uriVariables);
So it becomes..
String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng={latlng}&key={key}";
Map<String, Object> uriVariables = new HashMap<>();
uriVariables.put("latlng", "5.47686,-73.961452");
uriVariables.put("key", "YOUR_API_KEY");
String consumeJSONString = restTemplate.getForObject(url, String.class, uriVariables);
As i got a solution to dynamic variable
where this is a Rest Url
#RequestMapping(value = "/studentdetail")
public User detailStudent(#RequestParam(value = "userid", required = true) String userid) throws SQLException { /*your task goes here*/}
and this is what i m sending the dynamic params as userid which can be anything hopefully it will help u a lot
URL url = new URL("http://localhost:9090/testpapers?userid=" + userid);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
String s1=URLEncoder.encode(postDataParams, "UTF-8");
writer.write(s1);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
writer.close();
reader.close();
}
If using Spring Framework, we have org.springframework.web.util.UriTemplate which does this in a structured way
Default URI in application.properties
we can define a parametrized uri in properties file. For eg
url.template.google.maps=https://maps.googleapis.com/maps/api/geocode/json?latlng={latitude},{longitude}&key={api_key}
Initialize UriTemplate
The template that we defined in properties file can now be read in a #PostConstruct block and stored in UriTemplate variable. I am saying #PostConstruct just to save trips to fetch the template for each request
#Value("${url.template.google.maps}")
private String googleMapsUri;
private UriTemplate googleMapsServiceUriTemplate;
#PostConstruct
private void init() throws URISyntaxException {
googleMapsServiceUriTemplate= new UriTemplate(googleMapsUri);
}
Expand variables and get URI
Now that we have everything, let's expand and get the uri
Given these values, call below method to get the URI
String latitudeValue = "5.47686";
String longitudeValue = "-73.961452";
String apiKeyValue = "api_key_value";
Below method would take the necessary values, create a map with these values and then expand the uri template to populate the values. We get a nicely structured URI this way.
pubic URI getGoogleMapsServiceUri(latitudeValue, longitudeValue, apiKeyValue) {
Map<String, String> templateVariables = new HashMap<>();
templateVariables.put("latitude", latitudeValue); //could use a variable here
templateVariables.put("longitude", longitudeValue); //could use a variable here
templateVariables.put("api_key", apiKeyValue); //could use a variable here
URI googleMapsServiceUri = googleMapsServiceUriTemplate.expand(templateVariables);
System.out.println("URL is " + googleMapsServiceUri.getPath());
return googleMapsServiceUri;
}
I am completely new in RestTemplate and basically in the REST APIs also. I want to retrieve some data in my application via Jira REST API, but getting back 401 Unauthorised. Found and article on jira rest api documentation but don't really know how to rewrite this into java as the example uses the command line way with curl. I would appreciate any suggestion or advice how to rewrite:
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
into java using spring rest template. Where the ZnJlZDpmcmVk is a base64 encoded string of username:password. Thank you very much.
Taken from the example on this site, I think this would be the most natural way of doing it, by filling in the header value and passing the header to the template.
This is to fill in the header Authorization:
String plainCreds = "willie:p#ssword";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
And this is to pass the header to the REST template:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<Account> response = restTemplate.exchange(url, HttpMethod.GET, request, Account.class);
Account account = response.getBody();
You may use spring-boot RestTemplateBuilder
#Bean
RestOperations rest(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.basicAuthentication("user", "password").build();
}
See documentation
(before SB 2.1.0 it was #basicAuthorization)
There are multiple ways to add the basic HTTP authentication to the RestTemplate.
1. For a single request
try {
// request url
String url = "https://jsonplaceholder.typicode.com/posts";
// create auth credentials
String authStr = "username:password";
String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());
// create headers
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
// create request
HttpEntity request = new HttpEntity(headers);
// make a request
ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.GET, request, String.class);
// get JSON response
String json = response.getBody();
} catch (Exception ex) {
ex.printStackTrace();
}
If you are using Spring 5.1 or higher, it is no longer required to manually set the authorization header. Use headers.setBasicAuth() method instead:
// create headers
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("username", "password");
2. For a group of requests
#Service
public class RestService {
private final RestTemplate restTemplate;
public RestService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.basicAuthentication("username", "password")
.build();
}
// use `restTemplate` instance here
}
3. For each and every request
#Bean
RestOperations restTemplateBuilder(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.basicAuthentication("username", "password").build();
}
I hope it helps!
As of Spring 5.1 you can use HttpHeaders.setBasicAuth
Create Basic Authorization header:
String username = "willie";
String password = ":p#ssword";
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
...other headers goes here...
Pass the headers to the RestTemplate:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<Account> response = restTemplate.exchange(url, HttpMethod.GET, request, Account.class);
Account account = response.getBody();
Documentation:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpHeaders.html#setBasicAuth-java.lang.String-java.lang.String-
(maybe) the easiest way without importing spring-boot.
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user", "password"));
Reference Spring Boot's TestRestTemplate implementation as follows:
https://github.com/spring-projects/spring-boot/blob/v1.2.2.RELEASE/spring-boot/src/main/java/org/springframework/boot/test/TestRestTemplate.java
Especially, see the addAuthentication() method as follows:
private void addAuthentication(String username, String password) {
if (username == null) {
return;
}
List<ClientHttpRequestInterceptor> interceptors = Collections
.<ClientHttpRequestInterceptor> singletonList(new BasicAuthorizationInterceptor(
username, password));
setRequestFactory(new InterceptingClientHttpRequestFactory(getRequestFactory(),
interceptors));
}
Similarly, you can make your own RestTemplate easily
by inheritance like TestRestTemplate as follows:
https://github.com/izeye/samples-spring-boot-branches/blob/rest-and-actuator-with-security/src/main/java/samples/springboot/util/BasicAuthRestTemplate.java
Instead of instantiating as follows:
TestRestTemplate restTemplate = new TestRestTemplate();
Just do it like this:
TestRestTemplate restTemplate = new TestRestTemplate(user, password);
It works for me, I hope it helps!
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
then continue with the same procedure mentioned by the others here:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
request, String.class);
Use setBasicAuth to define credentials
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("myUsername", myPassword);
Then create the request like you prefer.
Example:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
request, String.class);
String body = response.getBody();
I'm using spring version 5.3.15 for my unit test environment. I used withBasicAuth for my tests :
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyTestClass {
...
#Autowired
private TestRestTemplate restTemplate;
...
#Test
#SneakyThrows
public void TestGetSettings(){
DtoClass dtoClass = this.restTemplate
.withBasicAuth(UserServices.DEFAULT_USER, UserServices.DEFAULT_PASSWORD)
.getForObject(String.format("http://localhost:%d/setting",
port), DtoClass.class);
assertThat(dtoClass.getClientAddress()).isNotEmpty();
}
...
}
As you see this method only work for basic authentication. If you look at the details of the withBasicAuth method, you will find that the method source will be like this:
// TestRestTemplate.java file:
...
public class TestRestTemplate {
...
private final RestTemplateBuilder builder;
...
public TestRestTemplate withBasicAuth(String username, String password) {
TestRestTemplate template = new TestRestTemplate(this.builder, username, password, this.httpClientOptions);
...
}
}
As a result, for other types of authentication you can use the RestTemplateBuilder as a builder which is mentioned in other answers.
Follow Step By Step
I added Client Credentials In application.Properties file like below...
http.basicauth.username = yourUserName
http.basicauth.password = yourPassword
And , Then I created one class With two fields Because I'm loading those two fields from the Application.Properties file : username and password . Make sure your class is annotated with #Component..
#Value("${http.basicauth.username}")
private String username;
#Value("${http.basicauth.password}")
private String password;
And Then , You need to autowired above class From Wherever you want..
// I'm getting a username and password from application.properties file
String userCredentials = referenceClassName.getUsername()+":"+referenceClassName.getPassword();
// Encoded User Credentials and Convert it into a String
String encodedUserCredentials= Base64.getMimeEncoder().encodeToString(userCredentialsBytes.getBytes());
headers.set("Authorization", "Basic " +base64UserCredentials);
HttpEntity request = new HttpEntity(headers);
String url = "externalUrl";
// Getting a Json String body
String body = restTemplate.exchange(url,HttpMethod.GET,request,String.class).getBody();
Note :: For getting an Access Token from String Json body , That's why I converted it into a Json Object
JsonObject tokenJsonObject = new JsonParser().parse(body).getAsJsonObject();
// Getting access token as string from tokenJsonObject
String accessToken = tokenJsonObject.has("access_token") && !tokenJsonObject.get("access_token").isJsonNull() ? tokenJsonObject.get("access_token").getAsString() : "";
If you have any concerns, please let me know in comments..Hope It will helpful to you..
I have been using Spring above 5.1.x
// create headers for basic auth
var headers = new HttpHeaders();
headers.setBasicAuth("username", "password");