【问题标题】:Jersey fluent API troublesJersey fluent API 问题
【发布时间】:2015-06-24 18:17:25
【问题描述】:

我正在重写一些代码,因为我必须升级到最新版本的 Jersey (2.18),但我不明白为什么 fluent API 会以这种方式工作。

为什么会编译:

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

但这不是:

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?

作为参考,这是我的整个方法,它曾经在 Jersey 2.5.1 中运行良好,但由于 getStatusInfo() 方法,甚至无法在 2.18 中编译:

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");
    }
}

【问题讨论】:

    标签: java jersey-2.0 fluent-interface jersey-client


    【解决方案1】:

    正如我在 jersey API 中发现的那样,返回状态信息的不是响应,而是应该是 Response.StatusType,这同样适用于 getFamily。

    查看 jersey api,请搜索 Response.class https://jersey.java.net/apidocs/latest/jersey/index.html

    【讨论】:

      【解决方案2】:

      这原来是一个 Maven 问题。我对 jersey-server 和 jersey-client 有自己的依赖项,它们都是 2.18 版本,但另一个依赖项对 jersey-1.9.1 有自己的依赖项。我以为我已经排除了它们,但无论如何它们都在我的图书馆列表中。我终于意识到我在排除项中使用了错误的包名。我将排除的 groupId 从 org.glassfish.jersey.core 更改为 com.sun.jersey,现在我的代码可以编译了。

      【讨论】:

        猜你喜欢
        • 2011-05-29
        • 1970-01-01
        • 2010-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多