Spring

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.

Create a new RestTemplate 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(greeting.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> response2 = restTemplate.getForEntity("http://localhost:8080", Greeting.class);
Greeting pojo = response2.getBody();
System.out.println(pojo.getContent());
}
//output:
Hello World!

Of cource it's possible to POST, PUT and use the other HTTP methods, as well as handling HTTP Headers

HttpEntity<Greeting> entity = new HttpEntity<Greeting>(new Greeting());
entity.getHeaders().add("Connection", "keep-alive");
restTemplate.exchange("http://localhost:8080", HttpMethod.POST, entity, Void.class);


Here are the Maven dependencies you need to use Spring as the client library for your load tests


<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.0.2.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.9.13</version>
</dependency>