3

I am trying to pass multiple entities to the web service method. The web service method has two parameters of pojo entity type. I am able to post only one entity to the web service method. I am unable to post multiple entities to the web service method.

        Server side code:

        @POST
        @Path("/test")
        @Produces(MediaType.APPLICATION_XML)
        @Consumes(MediaType.APPLICATION_XML)
        public void testMethod(Emp emp, Student stud){
        ...
        }

        Client side code:
        ...
    ...
Emp emp = new Emp;
Student stud = new Student();
        ClientResponse response = resource.type(MediaType.APPLICATION_XML).entity(emp).entity(stud).post(ClientResponse.class);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
123r789
  • 1,600
  • 3
  • 22
  • 33
  • 1
    Indeed, you cannot - for good reasons. http://stackoverflow.com/questions/5553218/jax-rs-post-multiple-objects . Note: take special note of the answer of 'tine2k' which will probably easily be a working solution for you. – Gimby Apr 14 '15 at 13:22
  • [Bottom of @Gimby's link](http://stackoverflow.com/a/29307655/2587435) - an option – Paul Samsotha Apr 14 '15 at 14:08

1 Answers1

2

A request can only have one entity body, that's why the restriction. The only option I can think of is to use multipart request, where you can have multiple body parts.

Example server side

@Path("multipart")
public class MultipartResource {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response doPost(@FormDataParam("emp") Emp emp,
                           @FormDataParam("student") Student student) {
        StringBuilder builder = new StringBuilder();
        builder.append("Emp:").append(emp.name).append("\n");
        builder.append("Student:").append(student.name).append("\n");
        return Response.ok(builder.toString()).build();
    }

    public static class Student {
        public String name;
    }

    public static class Emp {
        public String name;
    }
}

Client side

public class Main {
    public static void main(String[] args) throws Exception {
        Client client = Client.create();

        Emp emp = new Emp();
        emp.name = "pee";

        Student stu = new Student();
        stu.name = "skillet";

        FormDataMultiPart multipart = new FormDataMultiPart()
                .field("emp", emp, MediaType.APPLICATION_JSON_TYPE)
                .field("student", stu, MediaType.APPLICATION_JSON_TYPE);

        final String url = "http://localhost:8080/api/multipart";
        String response = client.resource(url).type(MediaType.MULTIPART_FORM_DATA_TYPE)
                .post(String.class, multipart);
        System.out.println(response);   
    }
}

Result:

Emp:pee
Student:skillet

Jersey dependency for multipart support.

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>${jersey1.version}</version>
</dependency>
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for the answer. I have another problem i.e I am sending the list of Emp entities from the server. At client side i am trying to get those list of entities. But it is giving unmarshal excepiton. Why it is adding 's' at the end of elementname i.e "emps" instead of "emp". – 123r789 Apr 17 '15 at 05:58
  • @XmlRootElement public class Emp{ ... } Exception is: ....unmarshalexception unexpected element(url="" local="emps") expected element {}emp CLIENTSIDE CODE: ClientResponse response = resource.type(MediaType.APPLICATION_XML).post(ClientResponse.class); List li = response.getEntity(new GenericType>(Emp.class)); – 123r789 Apr 17 '15 at 05:59
  • Complete question is in this link: http://stackoverflow.com/questions/29647238/how-to-send-the-list-of-entities-from-the-jersey-service-endpoint – 123r789 Apr 17 '15 at 06:06