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
671EEA7C86
ReplyDeleteCam Åžov
Görüntülü Sex
Görüntülü Şov
JAX-RS provides a convenient API for communicating with RESTful services from Java applications. The article demonstrates how to create a JAX-RS client using Jersey and configure connection and response timeouts. The example is built with Spring Boot and shows the dependencies, REST service, client implementation, and resulting response.
DeleteThe example uses Jersey Client APIs to communicate with a REST endpoint and demonstrates configuring both connection and read timeouts. The client sets a 40-second connection timeout and a 40-second response timeout before sending a GET request. This practical use of JAX-RS and Jersey is relevant to JAX-RS ReactJS Full Stack Developer Course.
The article also shows how the client receives the HTTP status and response body from the REST service. The sample service accepts a name as a path variable and returns a welcome message, while the client prints the status code, status message, and returned content. This REST-client and full-stack integration approach is also applicable to JAX-RS NextJS Full Stack Developer Course.
Delete05CC4A0A1F
ReplyDeleteGörüntülü Sex
Sanal Sex
Cam Show
F3F63C85
ReplyDeleteMuÅŸ
MuÄŸla
Şırnak
Bartın
Urfa
Bingöl
Aydın
Zonguldak
Burdur