jersey-client

How to print or log Jersey client request?


I am trying to print or log the request I am making from Jersey. I think it might not be including the body and the server is rejecting my request because it is empty. I found instructions from other answers, but I am not getting the results I am looking for.

Here is my test code:

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.client.JerseyWebTarget;
import org.glassfish.jersey.client.JerseyInvocation.Builder;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.logging.LoggingFeature.Verbosity;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Feature;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

public class JerseyTest {
  public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Logger logger = Logger.getLogger(JerseyTest.class.getName());
    Feature feature = new LoggingFeature(logger,Level.ALL,Verbosity.PAYLOAD_ANY,50000);
    config.register(feature);
    
    JerseyClient client = JerseyClientBuilder.createClient(config);
    
    JerseyWebTarget target = client.target("https://api.alt.www4.irs.gov/auth/oauth/v2/token");
    
    HashMap<String,String> postBody = new HashMap<String,String>();
    postBody.put("grant_type","urn:ietf:params:oauth:grant-type:jwt-bearer");
    postBody.put("assertion","1234567890");
    postBody.put("client_assertion_type","urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
    postBody.put("client_assertion","1234567890");
    
    Builder builder = target.request(MediaType.APPLICATION_FORM_URLENCODED);
    Response response = builder.post(Entity.entity(postBody, MediaType.APPLICATION_JSON));
    
    System.out.println(response.getStatus());
    System.out.println(response.readEntity(String.class));
  }
}

I get the response output, but I don't see the request:

400
{
  "error_code":"ESRV103",
  "error_msg": {
  "error":"invalid_request",
  "error_description":"Missing or duplicate parameters"
}
}

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.propfinancing</groupId>
  <artifactId>IRSIntegration</artifactId>
  <version>0.0.1</version>
  <dependencies>
    <dependency>
      <groupId>com.propfinancing</groupId>
      <artifactId>pflib</artifactId>
      <version>1.0.0</version>
    </dependency>  
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.19.0</version>
    </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>3.1.0</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>3.1.0</version>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>3.1.0</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.7.6</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.0</version>                
        <configuration>
                <release>9</release>
                </configuration>
      </plugin>
      <plugin>
       <artifactId>maven-jar-plugin</artifactId>
        <version>3.3.0</version>
       <configuration>
         <archive>
           <addMavenDescriptor>false</addMavenDescriptor>
         </archive>
       </configuration>
     </plugin>
    </plugins>
  </build>
</project>

The Spring dependency is in there because I also tried using their client, but that was too hard to use. I assume it will not interfere with anything.

Any idea why this is not working?

Thanks, Neil


Solution

  • I was able to figure this out. First, I created a LoggingFilter:

    import java.io.IOException;
    
    import jakarta.ws.rs.client.ClientRequestContext;
    import jakarta.ws.rs.client.ClientRequestFilter;
    
    public class LoggingFilter implements ClientRequestFilter {
      public void filter(ClientRequestContext requestContext) 
      throws IOException {
        System.out.println(requestContext.getHeaders());
        System.out.println(requestContext.getEntity().toString());
      }
    }
    

    And then register it on the client:

    import java.util.HashMap;
    import jakarta.ws.rs.client.Client;
    import jakarta.ws.rs.client.ClientBuilder;
    import jakarta.ws.rs.client.Entity;
    import jakarta.ws.rs.client.Invocation;
    import jakarta.ws.rs.client.WebTarget;
    import jakarta.ws.rs.core.Form;
    import jakarta.ws.rs.core.MediaType;
    import jakarta.ws.rs.core.Response;
    
    public class JerseyTest {
      public static void main(String[] args) {
        Client client = ClientBuilder.newClient();
        client.register(new LoggingFilter());
        
        WebTarget target = client.target("https://api.alt.www4.irs.gov/auth/oauth/v2/token");
    
        Form form = new Form();
        form.param("grant_type","urn:ietf:params:oauth:grant-type:jwt-bearer");
        form.param("assertion","1234567890");
        form.param("client_assertion_type","urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
        form.param("client_assertion","1234567890");
        
        Invocation.Builder builder = target.request(MediaType.APPLICATION_FORM_URLENCODED);
        Response response = builder.post(Entity.entity(form, MediaType.APPLICATION_JSON));
        
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));
      }
    }