Post 将json发布到外部api

Post 将json发布到外部api,post,jersey,dropwizard,api-design,jersey-client,Post,Jersey,Dropwizard,Api Design,Jersey Client,我刚刚开始使用Drropwizard,希望将json数据提交给POST方法 @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public String newPost(){ Client client = ClientBuilder.newClient(); String input = "{"version":"v1","buildTime":"2017-06-

我刚刚开始使用Drropwizard,希望将json数据提交给POST方法

@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


任何意见/建议都将不胜感激。

我更喜欢在使用
MediaType.APPLICATION\u JSON
时定义模型

样品

InputPOJO {
    String version;
    Long buildTime;
    // ...getters and setters here
}
然后使用
Response
实体将响应对象返回为(为清晰起见,包括导入)——


使用对@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;
}

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

作为参考,我最终使用了Jersey客户端,如下所示

进口清单:

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
在我的资源中使用@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;

我想这是为了实现POST方法本身,但我正在尝试在POST方法中使用另一个(外部)api(正如我在原始文章中所评论的)。你能指出如何在我的post方法中使用外部api吗?另外,我没有定义模型的原因是输入JSON可能会改变(无论如何,我需要处理原始JSON)。谢谢
    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;