In this tutorial we will learn how to use JAX-rs API with jersey client API to communicate with Restful service.
Response status code: 200
Response status: OK
Welcome Test user
Maven dependencies
I have created this example using the Spring boot application, so I need only starter dependencies which are listed as below.<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>If you are not using spring application then you need to add below dependencies in your pom.
<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</dependency> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</dependency> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</dependency> <scope>runtime</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotaions</dependency> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</dependency> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</dependency> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotaions</dependency> </dependency>
Rest service code
For the demonstrations purpose I have created a simple service which takes the input as path variable and returns a text as response.@RestController @RequestMapping("/hello") public class HelloService { @RequestMapping("/welcome/{name}") @GET @Produces(MediaType.APPLICATION_JSON) public String welcome(@PathVariable("name") String name) { return "Welcome "+name; } }
Rest client code
Below is the client code which makes call to rest service and prints the response to console. It also specifies connectivity and read timeout which results in timeout exception if it is not able to connect in given milliseconds or it is not able to get the response in specified milliseconds.//Annotated as component to be managed by the spring context @Component public class HelloClient { //this annotation guarantees the invocation of this method //once the spring boot application is up and running @EventListener(ApplicationReadyEvent.class) public void clientHello() { Client client = ClientBuilder.newClient(); //below code sets timeout for server connectivity client.property(ClientProperties.CONNECT_TIMEOUT, 40000); //setting timeout for service response client.property(ClientProperties.READ_TIMEOUT, 40000); Response resp = client.target("http://localhost:8080/hello/") .path("welcome") .path("Test user") .request(MediaType.APPLICATION_JSON).get(); StatusType status = resp.getStatusInfo(); System.out.println("Response status code: "+status.getStatusCode()); System.out.println("Response status: "+status.getReasonPhrase()); System.out.println(resp.readEntity(String.class)); } }Below is the output of above client.
Response status code: 200
Response status: OK
Welcome Test user
Comments
Post a Comment