Jersey fluent API troubles

284 views Asked by At

I'm rewriting some code because I had to upgrade to the latest version of Jersey (2.18) and I don't understand why the fluent API is working the way it does.

Why does this compile:

Response.Status.Family responseFamily = webTarget
    .request(MediaType.APPLICATION_JSON)
    .post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE))
    .getStatusInfo()
    .getFamily();

But this doesn't:

Response response = webTarget
    .request(MediaType.APPLICATION_JSON)
    .post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE));

response.getStatusInfo().getFamily();  // the method getStatusInfo() isn't available here; why not?

For reference, here's my whole method that used to work fine in Jersey 2.5.1 but won't even compile in 2.18 because of the getStatusInfo() method:

public void postGenericJson(Object entity, String... pathSegments) {
    Response response;
    WebTarget webTarget = httpsClient.target(apiBaseUrl);

    try {
        for (String pathSegment : pathSegments) {
            webTarget = webTarget.path(pathSegment);
        }

        response = webTarget.request(MediaType.APPLICATION_JSON)
                .post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE));

        log.trace("Issued POST to '" + webTarget.getUri().toString() + "'.");
    } catch (Exception e) {
        log.error("Error posting DTO: POST " + webTarget.getUri().toString(), e);
        throw new RuntimeException("Error posting DTO.");
    }

    if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
        throw new ResponseProcessingException(response, "Error POSTing DTO");
    }
}
2

There are 2 answers

0
Joe Ernst On BEST ANSWER

This turned out to be a Maven issue. I had my own dependency for jersey-server and jersey-client, both at version 2.18, but another dependency had its own dependency on jersey-1.9.1. I thought I had excluded them, but they were in my library list anyway. I finally realized I had used the wrong package name in my exclusion. I changed the excluded groupId to com.sun.jersey from org.glassfish.jersey.core and now my code compiles.

0
kamil.rak On

As I found in the jersey API it is not response that returns the status info, but it should rather be Response.StatusType, the same should apply to the getFamily.

See jersey api, pleach do search for Response.class https://jersey.java.net/apidocs/latest/jersey/index.html