2017-06-11 215 views
0

我剛開始使用Drropwizard並希望提交json數據到POST方法。發佈json到外部API

@POST 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public String newPost(){ 
    Client client = ClientBuilder.newClient(); 

    String input = "{"version":"v1","buildTime":"2017-06-06"}"; 

    //call external api with json_input 


    return result; 
} 

所以我想發佈輸入(原始json)到外部api。使用client.target("https://path_to_external_api").request().get(String.class);工作正常GET方法,但不知道如何實現POST

任何的意見/建議

表示讚賞。

回答

0

作爲參考,我最終使用了這樣的Jersey客戶端。使用方法@POST內的客戶端在我的資源

​​

:進口

列表

Client client = ClientBuilder.newClient(); 

    WebTarget tar = client.target("https://path_to_external_api"); 
    Response res = tar.request().accept(MediaType.APPLICATION_JSON) 
      .post(Entity.entity(json_input, MediaType.APPLICATION_JSON), Response.class); 
    return res; 
0

我寧願在使用MediaType.APPLICATION_JSON時定義模型。

樣品

InputPOJO { 
    String version; 
    Long buildTime; 
    // ...getters and setters here 
} 

隨後使用Response實體返回響應對象(包括進口爲清楚起見) -

import javax.ws.rs.POST; 
import javax.ws.rs.Produces; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.Path; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response;  


@POST 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
@Path("/xyz") 
public Response newPost(InputPOJO inputPOJO) { 
    String output = "Success!" + inputPOJO.getVersion(); 
    return Response.status(200).entity(output).build(); 
} 
+0

這我想這對實現POST方法本身的問題,但我試圖打POST方法內的另一個(外部)api(在我的原始文章中發表評論)。你能指出如何在我的post方法中打外部api嗎?此外,我沒有定義模型的原因是輸入JSON可能會改變(無論如何,我需要處理原始JSON)。謝謝 – Mahyar

0

使用由@nullpointer定義的服務的修改:

@Path("/testpostjson") 
public class MyPostResource { 

    public MyPostResource() { 

    }   
    @POST 
    @Produces(MediaType.APPLICATION_JSON) 
    @Consumes(MediaType.APPLICATION_JSON) 
    public Response newPost(InputPOJO inputPOJO) { 
     String output = "Success! " + inputPOJO.getVersion() +" "+ inputPOJO.getBuildTime(); 
     return Response.status(200).entity(output).build(); 
    } 
} 

我創建了這個客戶端,你可以使用:

@Produces(MediaType.TEXT_PLAIN) 
@Path("/client") 
public class Client2Post { 

    private Client client; 

    public Client2Post(Client client) { 
     this.client = client; 
    } 

    @Path("/test") 
    @POST 
    @Consumes(MediaType.APPLICATION_JSON) 
    public String newPost(){ 

     String input = "{\"version\":\"v1\",\"buildTime\":\"2017-06-06\"}"; 

     //call external api with json_input 
     final Invocation.Builder request = client.target("http://localhost:8080/testpostjson").request(); 
     final Response result = request.post(Entity.entity(input, MediaType.APPLICATION_JSON_TYPE));  

     return result.readEntity(String.class); 

    } 
} 

還記得配置Jersey客戶端在你的配置文件:

@Valid 
@NotNull 
private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration(); 

@JsonProperty("jerseyClient") 
public JerseyClientConfiguration getJerseyClientConfiguration() { 
    return jerseyClient; 
} 

而在你的應用程序文件中註冊創建的資源。