I have written a Restful web service API, which is accepting two different Object, Is it possible to call this api using Jersey client. I am not able to call this using Jersey client. Is this a limitation of Rest API that we can not pass multiple objects to a method.
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/hello")
public class TimePassService {
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveEmployeeInfo(final Employee input,final Manager input1) {
String result = "Employee saved : " + input;
System.out.println(input);
System.out.println(input1);
return Response.status(201).entity(result).build();
}
}
When I discussed this with some techies, they replied that it is not possible, The solution is to wrap these two object into a third object and then pass a single Object.
Please let me know if there is some other solution of this.
That is not possible. See the JAX-RS specification:
3.3.2.1 Entity Parameters
The value of a parameter not annotated with @FormParam
or any of the annotations listed in in Section 3.2, called the entity parameter, is mapped from the request entity body. Conversion between an entity body and a Java type is the responsibility of an entity provider, see Section 4.2. Resource methods MUST have at most one entity parameter.
There can be only one method 'entity parameter'.
What you ask for would not be RESTful. REST ist not RPC (Remote Procedure Call), you don't 'pass' objects to a 'method'. In REST you transfer Resource representations from and to identifying URLs.
In your example the Resource would be an EmployeeInfo
wrapping Employee
and Manager
.
Besides, /post
is not a very RESTful URL. What Resource is identified by this? What happens if you GET /post
? Please think in REST terms, not in RPC.
The solution is to wrap these two object into a third object and then pass a single Object.
I wonder string json that post from client how does it look like?
is it this :
[
employee: {
"name": "Sury",
"age": 23
},
manager: {
"name": "Beack",
"Room": "DC2"
}
]