I have a method;
@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
public void test(ObjectOne objectOne, ObjectTwo objectTwo)
Now I know I can post a single object in json format, just putting it into the body. But is it possible to do multiple objects? If so, how?
If we look at what the OP is trying to do, he/she is trying to post two (possibly unrelated) JSON objects. First any solution to try and send one part as the body, and one part as some other param, IMO, are horrible solutions. POST data should go in the body. It's not right to do something just because it works. Some work-arounds might be violating basic REST principles.
I see a few solutions
1. Use application/x-www-form-urlencoded
Another option is to just use
application/x-www-form-urlencoded
. We can actually have JSON values. For examleThe one thing we need to get this to work is a
ParamConverterProvider
to get this to work. Below is one that has been implemented by Michal Gadjos of the Jersey Team (found here with explanation).If you aren't scanning for resource and providers, just register this provider, and the above example should work.
2. Use Multipart
One solution that no one has mentioned, is to use multipart. This allows us to send arbitrary parts in a request. Since each request can only have one entity body, multipart is the work around, as it allows to have different parts (with their own content types) as part of the entity body.
Here is an example using Jersey (see official doc here)
Dependency
Register the
MultipartFeature
Resource class
Now the tricky part with some clients is that there isn't a way to set the
Content-Type
of each body part, which is required for the above to work. The multipart provider will look up message body reader, based on the type of each part. If it's not set toapplication/json
or a type, theFoo
orBar
has a reader for, this will fail. We will use JSON here. There's no extra configuration but to have a reader available. I'll use Jackson. With the below dependency, no other configuration should be required, as the provider will be discovered through classpath scanning.Now the test. I will be using cURL. You can see I explicitly set the
Content-Type
for each part withtype
. The-F
signifies to different part. (See very bottom of the post for an idea of how the request body actually looks.)The result is exactly as we expected. If you look at the resource method, all we do is return this string
foo.foo + "; " + bar.bar
, gathered from the two JSON objects.You can see some examples using some different JAX-RS clients, in the links below. You will also see some server side example also from those different JAX-RS implementations. Each link should have somewhere in it a link to the official documentation for that implementation
There are other JAX-RS implementations out there, but you will need to find the documentation for it yourself. The above three are the only ones I have experience with.
As far as Javascript clients, most of the example I see (e.g. some of these involve setting the
Content-Type
to undefined/false (usingFormData
), letting the Browser handle the it. But this will not work for us, as the Browser will not set theContent-Type
for each part. And the default type istext/plain
.I'm sure there are libraries out there that allow setting the type for each part, but just to show you how it can be done manually, I'll post an example (got a little help from here. I'll be using Angular, but the grunt work of building the entity body will be plain old Javascript.
The interesting part is the
createRequest
function. This is where we build the multipart, setting theContent-Type
of each part toapplication/json
, and concatenating the stringifiedfoo
andbar
objects to each part. If you are unfamiliar multipart format see here for more info. The other interesting part is the header. We set it tomultipart/form-data
.Below is the result. In Angular I just used the result to show in the HTML, with
$scope.result = response.data
. The button you see was just to make the request. You will also see the request data in firebug3. Just wrap them in a single parent object
This option should be self explanatory, as others have already mentioned.
The next approach is usually applied in this kind of cases:
The answer is no.
The reason is simple: This about the parameters you can receive in a method. They must be related to the request. Right? So they must be either headers or cookies or query parameters or matrix parameters or path parameters or request body. (Just to tell the complete story there is additional types of parameters called context).
Now, when you receive JSON object in your request, you receive it in a request body. How many bodies the request may have? One and only one. So you can receive only one JSON object.
You can't put two separate objects in one single POST call as explained by Tarlog.
Anyway you could create a third container object that contains the first two objects and pass that one within the POS call.
I have also faced with these problem. Maybe this will help.
You can not use your method like this as correctly stated by Tarlog.
However, you can do this:
or this:
Furthermore, you can always combine your method with GET parameters: