In this post under JAX-RS Client, I will show with example how to add filters that will get executed before sending the request and after receiving the response.
For this purpose we will create two filters.
The below code shows the filter code that will be executed before the request is sent
1 package defaultPackage;
2
3 import java.io.IOException;
4
5 import jakarta.ws.rs.client.ClientRequestContext;
6 import jakarta.ws.rs.client.ClientRequestFilter;
7 import jakarta.ws.rs.ext.Provider;
8
9 public class MyClientRequestFilter implements ClientRequestFilter {
10 @Override
11 public void filter(ClientRequestContext clientRequestContext) throws IOException {
12 System.out.println("MyClientRequestFilter");
13 }
14 }
As you can see in the above filter code, at line 9, the class “MyClientRequestFilter” implements “ClientRequestFilter” interface.
The interface has one abstract method “filter” which is implemented by the class.
The current implementation only prints static text to the console.
The next code shows the filter code that will be executed after the response is received
1 package defaultPackage;
2
3 import java.io.IOException;
4
5 import jakarta.ws.rs.client.ClientRequestContext;
6 import jakarta.ws.rs.client.ClientResponseContext;
7 import jakarta.ws.rs.client.ClientResponseFilter;
8
9 public class MyClientResponseFilter implements ClientResponseFilter {
10 @Override
11 public void filter(ClientRequestContext clientRequestContext, ClientResponseContext clientResponseContext) throws IOException {
12 System.out.println("MyClientResponseFilter");
13 }
14 }
As you can see in the above filter code, at line 9, the class “MyClientResponseFilter” implements “ClientResponseFilter” interface.
The interface has one abstract method “filter” which is implemented by the class.
The current implementation only prints static text to the console.
Now we will use both the filter class in our main method
Main Class
1 package defaultPackage;
2
3 import jakarta.ws.rs.client.Client;
4 import jakarta.ws.rs.client.ClientBuilder;
5 import jakarta.ws.rs.client.Invocation;
6 import jakarta.ws.rs.client.WebTarget;
7 import jakarta.ws.rs.core.Response;
8
9 public class Example15 {
10 public static void main(String[] args) {
11 Client client = ClientBuilder.newClient();
12 try {
13 System.out.println("First WebTarget instance");
14 WebTarget webTarget = client.target("https://jsonplaceholder.typicode.com/users/2");
15 MyClientRequestFilter myClientRequestFilter = new MyClientRequestFilter();
16 MyClientResponseFilter myClientResponseFilter = new MyClientResponseFilter();
17 webTarget = webTarget.register(myClientRequestFilter);
18 webTarget = webTarget.register(myClientResponseFilter);
19 Invocation.Builder builder = webTarget.request();
20 Invocation invocation = builder.buildGet();
21 Response response = invocation.invoke();
22 if(response.getStatus() == 200) {
23 System.out.println("Successful");
24 String result = response.readEntity(String.class);
25 System.out.println(result);
26 } else {
27 System.out.println("Failed");
28 }
29 System.out.println("Second WebTarget instance");
30 webTarget = client.target("https://reqres.in/api/users/2");
31 builder = webTarget.request();
32 invocation = builder.buildGet();
33 response = invocation.invoke();
34 if(response.getStatus() == 200) {
35 System.out.println("Successful");
36 String result = response.readEntity(String.class);
37 System.out.println(result);
38 } else {
39 System.out.println("Failed");
40 }
41 } finally {
42 client.close();
43 }
44 }
45 }
In the above code, at line 15 and 16 we create an instance of “MyClientRequestFilter” and “MyClientResponseFilter” class.
We register the filters with the webtarget at line 17 and 18.
Now these filter will be called whenever WebTarget makes a request and gets response.
The code from line 13 to 28 shows example of an instance of WebTarget which has filter registered.
The code from line 29 to 40 shows example of an instance of WebTarget without any filter registerd.
Below is the output
Output
First WebTarget instance
MyClientRequestFilter
MyClientResponseFilter
Successful
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
Second WebTarget instance
Successful
{"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
As you see from the output when the first WebTarget instance is used to make a request, the filters are executed.
But when the second WebTarget instance is used to make a request, the filters are not executed as they are not registered.