java 用什么来代替 org.jboss.resteasy.client.ClientRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14458450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:20:28  来源:igfitidea点击:

What to use instead of org.jboss.resteasy.client.ClientRequest?

javajbossresteasy

提问by Torque

I just found that org.jboss.resteasy.client.ClientRequestis deprecated, invalidating everything I could find on Google about how to use the RESTEasyclient. The Javadocgives no indication as to what to use instead. Google is likewise silent.

我刚刚发现它org.jboss.resteasy.client.ClientRequest已被弃用,使我在 Google 上可以找到的有关如何使用RESTEasy客户端的所有信息都无效。在Javadoc中没有给出指示,以什么来代替使用。谷歌同样保持沉默。

I have reverted to 2.3.5for now, but would be interested in the answer anyways, as well as how one was supposed to find out the answer without asking someone else who knew - is there a resource with that information where I could have looked ?

2.3.5现在已经恢复了,但无论如何都会对答案感兴趣,以及人们应该如何在不询问其他知道的人的情况下找到答案 - 是否有包含该信息的资源我可以查看?

采纳答案by Doug Porter

The 3.0 beta documentation heredescribes these deprecations like so:

此处的 3.0 beta 文档描述了这些弃用,如下所示:

Resteasy manual client API, interceptors, StringConverters, StringParamterConverters, and Async HTTP APIs have all been deprecated and will be removed possibly in a later release. There is now a JAX-RS 2.0 equivalent for each of these things.

Resteasy 手动客户端 API、拦截器、StringConverters、StringParamterConverters 和 Async HTTP API 都已弃用,可能会在以后的版本中删除。现在有一个 JAX-RS 2.0 等效于这些东西。

This would imply that the preferred method will be to use the JAX-RS Client API described in this post

这将意味着,优选的方法将是使用在所描述的JAX-RS客户端API此篇

回答by Andy MacKinlay

If we assume there is a JSON API at http://example.org/pizza/{id}.json, (where 'id' is a pizza ID) which returns results such as

如果我们假设有一个 JSON API http://example.org/pizza/{id}.json,(其中 'id' 是一个披萨 ID)它会返回诸如

{ 
  "name": "Hawaiian",
  "toppings": ["tomato", "ham", "cheese", "pineapple"]
}

Building on the Invocation.BuilderJavadocs, we can do something like this,

Invocation.BuilderJavadocs 的基础上,我们可以做这样的事情,

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import org.glassfish.jersey.Hymanson.HymansonFeature;

public class PizzaClient {
    private Client client;

    public PizzaClient() {
         client = ClientBuilder.newClient();
         // enable POJO mapping using Hymanson - see
         // https://jersey.java.net/documentation/latest/user-guide.html#json.Hymanson
         client.register(HymansonFeature.class); 
    }

    /** POJO which maps to JSON results using Hymanson */
    public static class Pizza {
        private String name;
        private String[] toppings;

        public String getName() { return name; }
        public String[] getToppings() { return toppings ; }
    }

    public Pizza getPizzaById(String id) {
        String uri = String.format("http://example.org/pizza/%s.json", id)
        Invocation.Builder bldr = client.target(uri).request("application/json");
        return bldr.get(Pizza.class);
    }

    public static void main(String[] args) {
       PizzaClient pc = new PizzaClient();
       Pizza pizza = pc.getPizzaById("1");
       System.out.println(pizza.getName() + ":");
       for (String topping : pizza.getToppings()) {
            System.out.println("\t" + topping);
       }
    }
}

(this is also assisted by this postalthough that uses the deprecated API).

(这也得到了这篇文章的帮助,尽管它使用了已弃用的 API)。

Note also that you may need to register a special handler if you want to use Hymanson to read POJOs (or, I think, using JAXB) as documented here

另请注意,如果您想使用 Hymanson 读取 POJO(或者,我认为,使用 JAXB),您可能需要注册一个特殊的处理程序,如此处所述

UpdateYou actually only need the following Maven dependencies:

更新您实际上只需要以下 Maven 依赖项:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-Hymanson</artifactId>
        <version>2.3.1</version>
    </dependency>

(In which case you're not using RestEasy at all -- the javax.ws.rsJAXRS implementation comes from Jersey)

(在这种情况下,您根本不使用 RestEasy—— javax.ws.rsJAXRS 实现来自 Jersey)

ORyou can stick with JBoss:

或者你可以坚持使用 JBoss:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-Hymanson2-provider</artifactId>
        <version>3.0.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.0.4.Final</version>
    </dependency>

In which case you can just remove the HymansonFeature line in the above code, and the code uses the more liberal Apache licence.

在这种情况下,您可以删除上面代码中的 HymansonFeature 行,代码使用更自由的 Apache 许可证。

回答by guyr

The RESTEasy documentationsays we should close the client connection; that would be client.close()in your example. But every example I can find does not do this. Will the client connection get closed automatically during garbage collection?

的RESTEasy文档说我们应该关闭客户端连接; 那将client.close()在您的示例中。但是我能找到的每个例子都没有这样做。垃圾回收期间客户端连接会自动关闭吗?