RestTemplate is the REST client in the Spring framework and commonly used for RESTful communication
.
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
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>
Copyright © All Rights Reserved