1

I need to send a JSON body to https://mandrillapp.com/api/1.0//messages/send-template.json . How do I do this using RestEasy in Java? This is what I have so far:

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("https://mandrillapp.com/api/1.0//messages/send-template.json");

How do I actually send the JSON?

4

3 に答える 3

2

を取得したらResteasyWebTarget、 を取得する必要があります。Invocation

Invocation.Builder invocationBuilder = target.request("text/plain").header("some", "header");
Invocation incovation = invocationBuilder.buildPost(someEntity);
invocation.invoke();

someEntityのインスタンスはどこにありますかEntity<?>。で作成

Entity<String> someEntity = Entity.entity(someJsonString, MediaType.APPLICATION_JSON);

この javadoc を読んでください。

これは 3.0 ベータ 4 用です。

于 2013-08-29T20:53:43.287 に答える
1

これは少し古い質問ですが、Google で似たようなものを探していたので、これが RestEasy クライアント 3.0.16 を使用した私の解決策です。

Map オブジェクトを使用して送信しますが、 Jackson プロバイダーが JSON に変換できる任意の JavaBean を使用できます。

ところで、resteasy-jackson2-provider lib を依存関係として追加する必要があります。

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://server:port/api/service1");
Map<String, Object> data = new HashMap<>();
data.put("field1", "this is a test");
data.put("num_field2", 125);
Response r = target.request().post( Entity.entity(data, MediaType.APPLICATION_JSON));
if (r.getStatus() == 200) {
    // Ok
} else {
    // Error on request
    System.err.println("Error, response: " + r.getStatus() + " - "+ r.getStatusInfo().getReasonPhrase());
}
于 2016-05-12T11:18:19.397 に答える
0

私はこのフレームワークを使用したことがありませんが、この urlの例によれば、次のような呼び出しを行うことができるはずです:

        Client client = ClientBuilder.newBuilder().build();
        WebTarget target = client.target("http://foo.com/resource");
        Response response = target.request().get();
        String value = response.readEntity(String.class);
        response.close();  // You should close connections!

3行目はあなたが探している答えのようです。

于 2013-08-29T20:53:51.723 に答える