I'm trying to find a way to create a service that will receive different object types as JSON and unmarshall them correctly. Until now I was able to achieve single or list object unmarshalling using a custom deserializer and google's gson
library but this is really limiting. The only types of services I can create using this can only have a single type of object as a parameter, like so:
@GET
@Path("myService")
@Consumes(MediaType.APPLICATION_JSON)
public Response myService(MyEntity entity) {
//Random stuff using entity
return Response.ok().build();
}
However I'm trying to create a service like so:
@GET
@Path("advancedService")
@Consumes(MediaType.APPLICATION_JSON)
public Response advancedService(MyEntity1 entity1, MyEntity2 entity2, @QueryParam("normalParam") int normalParam, @QueryParam("normalBoolean") boolean normalBoolean) {
//Do stuff using both entity1 and entity2.
return Response.ok().build();
}
Right now, when I send a JSON through an ajax request, in order for the service to understand the object structure it must be formatted like so:
$.ajax({
url: 'services/MyServices/myService',
type: 'GET',
data: JSON.stringify(myEntityObjectStructure),
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function (result, status, xhr) {
//blahblah
}
});
In the data
configuration it needs to read the object structure only, otherwise it gets confused. I'm trying to send a request like this:
$.ajax({
url: 'services/MyServices/myService',
type: 'GET',
data: {
myEntity1: JSON.stringify(myEntity1ObjectStructure),
myEntity2: JSON.stringify(myEntity2ObjectStructure),
normalParam: param1,
booleanParam: param2
},
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function (result, status, xhr) {
//blahblah
}
});
But it just won't read the param names and it gets stuck since it thinks it must unmarshall all of the data
structure into a single object.
I'm using jersey 2.19. Also I'm looking for a normal solution not a 'hacky' one. I could just as well send the objects as a 'String' and unmarshall it myself but I want to stick to the service standards using annotations and having a custom serializer or some type of web.xml
configuration that takes care of the (un)marshalling. I like using gson
for its simplicity (especially with date formats and not requiring annotations for field exclusion etc) but I'm open to use Jackson
as well.
EDIT: It can also be a POST
type of service, I just need it to have 2 different classes as parameters.