Terance Wijesuriya
Terance Wijesuriya

Reputation: 1986

How to pass multiple parameters to Jersey POST method

I am trying to pass multiple parameters to Jersey POST method . Currently I am following below steps to pass a single parameter to Jersey POST method.

Client client = ClientBuilder.newClient();
WebTarget target= client.target("http://localhost:8080/Rest/rest/subuser").path("/insertSubUser");

SubUserBean subUserBean=new SubUserBean();
subUserBean.setIdUser(1);
subUserBean.setIdSubUserType(1);
subUserBean.setIdSubUser(15);
subUserBean.setFirstName("Haritha");
subUserBean.setLastName("Wijerathna");
subUserBean.setNumberOfDaysToEditRecord(14);
subUserBean.setUserName("haritha");
subUserBean.setPassword("hariwi88");
subUserBean.setDateCreated(Common.getSQLCurrentTimeStamp());
subUserBean.setLastUpdated(Common.getSQLCurrentTimeStamp());

target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(subUserBean, MediaType.APPLICATION_JSON_TYPE));

SubUserJSONService.java

@Path("/subuser")
public class SubUserJSONService {

    @POST
    @Path("/insertSubUser")
    @Consumes(MediaType.APPLICATION_JSON)
    public String updateSubUser(SubUserBean bean){

        SubUserInterface table = new SubUserTable();
        String result= table.insertSubUser(bean);
        return result;
    }
}

Now, I want to pass parameters to following method via Jersey POST method.

public String insertHistory(List<SocialHistoryBean> list, String comment){
    //my stuffs
}

Have any ideas to do above work ?

Thank you.

Upvotes: 3

Views: 27617

Answers (3)

Naor Bar
Naor Bar

Reputation: 2209

In case you're using Jersey 1.x, check this example on how to post multiple objects as @FormParam

Client: (pure Java):

public Response testPost(String param1, String param2) {
    // Build the request string in this format:
    // String request = "param1=1&param2=2";
    String request = "param1=" + param1+ "&param2=" + param2;
    WebClient client = WebClient.create(...);
    return client.path(CONTROLLER_BASE_URI + "/test")
            .post(request);
}

Server:

@Path("/test")
@POST
@Produces(MediaType.APPLICATION_JSON)
public void test(@FormParam("param1") String param1, @FormParam("param2") String param2) {
    ...
}

Upvotes: 2

dsp_user
dsp_user

Reputation: 2119

JSON data cannot be passed to the server in a List. This means that you should create a wrapper around your SocialHistoryBean class (i.e around the list that holds your objects)

 @XmlRootElement(name = "uw")
 public class SocialHistoryBeanWrapper implements Serializable {

private List<SocialHistoryBean> sList ;//this will hold your SocialHistoryBean instances
public SocialHistoryBeanWrapper(){
    sList = new ArrayList<User>();

    }
public List<User> getUsrList(){
    return sList;
}
    }

Your server side code will be like

@POST
@Path("/history")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String insertHistory( @QueryParam("comment") String comment, SocialHistoryBeanWrapper uw) {
    do whatever you want with your history data
    //userData.setUser(uw.getUsrList().get(0));

    return comment; //just echo the string that we have  sent from client

}

Note that comment is passed with @QueryParam (this means it's not part of the POST request (body) but is rather encoded in the URL string. For this to work, you can call your service as (the client code)

 WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8088/Rest/rest/subuser").build());    

SocialHistoryBeanWrapper uw = new SocialHistoryBeanWrapper();

      //just populate whatever fields you have;
        uw.getUsrList().get(0).setName("Mark Foster");
        uw.getUsrList().get(0).setProfession("writer");
        uw.getUsrList().get(0).setId(55);


        String s = target.path("history").queryParam("comment", "OK").request()
                   .accept(MediaType.TEXT_PLAIN).post(Entity.entity(uw, MediaType.APPLICATION_JSON), String.class);

        System.out.println(s);//this prints OK

Upvotes: 1

Sheetal Mohan Sharma
Sheetal Mohan Sharma

Reputation: 2924

You can try using MultivaluedMap.Add form data and send it to the server. An example below, code is not tested just for demo/logic flow.

WebTarget webTarget = client.target("http://www.example.com/some/resource");
    MultivaluedMap<List, String> formData = new MultivaluedHashMap<List, String>();
    formData.add(List, "list1");
    formData.add("key2", "value2");
    Response response = webTarget.request().post(Entity.form(formData));

Consume this on server side something like

@Path("/uripath")
@POST -- if this is post or @GET
@Consumes("application/x-www-form-urlencoded;charset=UTF-8") or json..
@Produces("application/json")
public void methodNameHere(@FormParam("list") List<String> list1, @FormParam("key2") String val2) {

    System.out.println("Here are I am");
    System.out.println("list1" + list1.size);
    System.out.println("val2" + val2);
}

Read more here in docs..

Upvotes: 6

Related Questions