Implementing client-side response message filters

You can build a client-side response filter by implementing the javax.ws.rs.client.ClientResponseFilter interface. This filter is used for manipulating the response message returned by the REST APIs.

The following code snippet shows the use of JAXRSClientResponseFilter to check the response received from the server and then log the error (if any) for audit purposes:

//Other imports are omitted for brevity 
import javax.ws.rs.client.ClientRequestContext; 
import javax.ws.rs.client.ClientResponseContext; 
import javax.ws.rs.client.ClientResponseFilter; 
 
public class JAXRSClientResponseFilter implements  
   ClientResponseFilter { 
 
    @Override 
    public void filter(ClientRequestContext reqContext,  
        ClientResponseContext respContext) throws IOException { 
        if (respContext.getStatus() == 200) { 
            return; 
        }else{ 
            logError(respContext); 
        } 
    } 
 
    private void logError(ClientResponseContext respContext) { 
        //Code for logging error goes here. 
    } 
} 

You can register the ClientResponseFilter interface to the JAX RS client by calling the register() method on the javax.ws.rs.client.Client object. An example is given here:

import javax.ws.rs.client.Client; 
import javax.ws.rs.client.ClientBuilder; 
 
Client client = ClientBuilder.newClient(); 
client.register(JAXRSClientResponseFilter.class);  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.194