首页 > 解决方案 > 扩展 HttpEntityEnclosureRequestBase 时出错

问题描述

我正在尝试制作一个自定义 GET 请求,该请求可以body用于第 3 部分 API。我为此编写了以下代码:

public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {

    public static final String METHOD_NAME = "GET";

    public HttpGetWithEntity() {
    }

    public HttpGetWithEntity(URI uri) {
        this.setURI(uri);
    }

    public HttpGetWithEntity(String uri) {
        this.setURI(URI.create(uri));
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}

问题是HttpEntityEnclosingRequestBase类使用以下注释:

@NotThreadSafe
public abstract class HttpEntityEnclosingRequestBase extends HttpRequestBase implements HttpEntityEnclosingRequest {

此扩展已从 中删除org.apache.http.annotation,因此在编译我的应用程序时,我收到以下错误:

cannot access org.apache.http.annotation.NotThreadSafe
class file for org.apache.http.annotation.NotThreadSafe not found

我怎样才能避免这种情况,因为我无法从谷歌搜索中找到一些东西并阅读一些其他正在扩展的代码HttpEntityEnclosingRequestBase。还有其他方法可以通过 GET 请求发送正文吗?

标签: javaspringhttp

解决方案


@ThreadSafeorg.apache.httpcomponents:httpcore将您的依赖版本更新到4.4.11或更高版本后,会发生找不到类编译错误。此httpcore版本附带:

Class org.apache.http.annotation.Immutable removed
Class org.apache.http.annotation.NotThreadSafe removed
Class org.apache.http.annotation.ThreadSafe removed
…

而不是这些被移除的类org.apache.http.annotation.Contract注解而org.apache.http.annotation.ThreadingBehavior enum被引入。

因此,现在您应该将@ThreadSafe事件重构为org.apache.http.annotation.Contract注释并传递 ThreadingBehavior枚举的值 SAFE 以指定线程安全的行为契约

@Contract(threading = ThreadingBehavior.SAFE)

@NotThreadSafe transforms to ThreadingBehavior.UNSAFE

@Immutable to ThreadingBehavior.IMMUTABLE or IMMUTABLE_CONDITIONAL

推荐阅读