Spring is one of widest used libraries to build applications with in Java. The spring-web module comes with the client RestTemplate that can be used to manage the most advanced HTTP communications.
RestTemplate is the client object you will use to make the http call. Despite the somewhat confusing name RestTemplate, this is actually a plain HTTP client that you can use for other things besides REST calls. Create an instance like this:
RestTemplate restTemplate = new RestTemplate();
Make a GET request and receive the response as a String
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080", String.class);
System.out.println(response.getBody());
//output:
{"id":1,"content":"Hello World!"}
Deserialization of the response to pojo's is easily done with the jackson dependencies
public static void main() {
ResponseEntity<Greeting> response = restTemplate.getForEntity("http://localhost:8080", Greeting.class);
Greeting greeting = response.getBody();
System.out.println(greeting.getContent());
}
//output:
Hello World!
Of cource it's possible to POST, PUT and use the other HTTP methods, as well as handling HTTP Headers
HttpHeaders headers = new HttpHeaders();
headers.add("Connection", "keep-alive");
HttpEntity<String> req = new HttpEntity<>("", headers);
ResponseEntity<String> resp = t.exchange(url, HttpMethod.GET, req, String.class);
If you want to call an endpoint over TLS, you need to create the RestTemplate so that it accepts the server certificate. There are a number of ways to do this.
The server certificate can be added as trusted by importing it into the Java cacerts. If the tests will execute at several machines, this may be an ardouous approach.
$ keytool -import -trustcacerts -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit -alias ServerToBeTested -import -file servercert.cer
You can create the RestTemplate client with a customized SSLContext and TrustStrategy. You can write your own logic which certificates to accept. Below example has a TrustStrategy that always returns true, meaning that everything is accepted. Keep in mind that always accepting everything is less secure. Therefore, use this approach with caution.
private RestTemplate getRestTemplateClientAuthentication() {
try {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = SSLContextBuilder
.create()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
HttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(client);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
If you need to authenticate using a client certificate (MTLS), you can modify the above SSLContext it to build it like below, here you specify the certificate file and the password
final String allPassword = "changeit";
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(ResourceUtils.getFile("classpath:clientkeystore.jks"), allPassword.toCharArray(),
allPassword.toCharArray())
.loadTrustMaterial(null, acceptingTrustStrategy).build();
Here are the Maven dependencies you need to use Spring as the client library for your load tests
org.springframework
spring-web
3.0.2.RELEASE
org.codehaus.jackson
jackson-mapper-asl
1.9.13