Getting Started with Visa APIs in Spring Boot
The following tutorial goes over how to set up Two-Way SSL authentication by sending a simple API request to https://sandbox.api.visa.com/vdp/helloworld.
Configure a Java Key Store
1. Make sure you download the 4 necessary files
- Private key (can only be downloaded once during the creation of a Visa Developer project)
- Project Certificate (a)
- VDP Certificate (b)
- DigiCert Certificate (c)
2. Follow these steps to generate a JKS file
Remember the password you will create!
Read more at https://developer.visa.com/pages/working-with-visa-apis/two-way-ssl.
Set up a Spring project
1. Initialize a project
Go to https://start.spring.io and set up the project including the Spring Web dependency
2. Create a class to store incoming data
Create a Response.java
class to contain the data
package com.example.demo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@JsonIgnoreProperties(ignoreUnknown = true)
public class Response { private String message;
public Response() {} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @Override
public String toString() {
return "Message=" + message;
}}
3. Update main application to fetch data
Add the .jks file and authentication credentials. Remember to update the keystore path and password, as well as user id and password!
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.ResourceUtils;
import javax.net.ssl.SSLContext;@SpringBootApplication
public class DemoApplication { private static final Logger log = LoggerFactory.getLogger(DemoApplication.class); public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
SSLContext sslContext = SSLContextBuilder
.create()
.loadKeyMaterial(ResourceUtils.getFile("src/main/resources/keyAndCertBundle.jks"), "password".toCharArray(), "password".toCharArray())
.loadTrustMaterial(ResourceUtils.getFile("src/main/resources/keyAndCertBundle.jks"), "password".toCharArray())
.build(); HttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.build(); return builder
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
.basicAuthentication("INSERT_USER_ID", "INSERT_PASSWORD")
.build();
} @Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> {
Response res = restTemplate.getForObject("https://sandbox.api.visa.com/vdp/helloworld", Response.class);
log.info(res.toString());
};
}}
4. Add dependency in pom.xml
Required to create an HttpClient in the RestTemplate builder
<dependency>
<groupId>org.apache.httpcomponents</groupId
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
5. Run the application
If authentication is successful, running ./mvnw spring-boot:run
should print to the console
Read more at https://spring.io/guides/gs/consuming-rest/.