0

I've just started using Drropwizard and want submit json data to POST method.

@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;
}

So I want to post the input (raw json) to external api.

using client.target("https://path_to_external_api").request().get(String.class); works fine for GET method but not sure how to implement POST

Any comments/suggestions is appreciated.

Mahyar
  • 817
  • 2
  • 10
  • 28

3 Answers3

0

I would prefer defining models while using MediaType.APPLICATION_JSON.

Sample

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

followed by using Response entity to return the Response object as(including imports for clarity) -

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();
}
Naman
  • 23,555
  • 22
  • 173
  • 290
  • This I guess is for implementation of POST method itself, but I'm trying to hit another (external) api inside the POST method (as commented on my original post). Could you please point out how to hit the external api in my post method? Also, the reason I'm not defining the model is input JSON could change (anyways I need to deal with raw json). Thanks – Mahyar Jun 12 '17 at 05:15
0

Using a modification of the service defined by @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();
    }
}

I created this client you can use:

@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);

    }
}

Also remember to configure the jersey client in your Configuration file:

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

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

And register the created resources in your Application file.

impulso
  • 86
  • 3
  • 7
0

For the reference, I ended up using Jersey client like this.

list of imports:

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;

using the client inside @POST method in my resource:

    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;
Mahyar
  • 817
  • 2
  • 10
  • 28