2017-06-11 35 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; 
} 

而在你的应用程序文件中注册创建的资源。