1

I'm using Jersey 2.10.4 and EclipseLink 2.5.2 (both bundled with GlassFish 4.1). I use MOXy for JSON (un)marshalling. I'd like to have a REST web service that consumes either a single resource or an array of multiple resources of the same type.

It is not possible to use two different JAX-RS methods with signatures like this (I replaced my actual resource/URL with a generic customer example - this class uses JAXB annotations) due to the ambiguity of the URL/media type combination (A resource model has ambiguous (sub-)resource method for HTTP method POST and input mime-types ...):

1)

@POST
@Path( "customers" )
@Consumes( { MediaType.APPLICATION_JSON } )
public void addCustomer( Customer newCustomer )
{
  [...]
}

2)

@POST
@Path( "customers" )
@Consumes( { MediaType.APPLICATION_JSON } )
public void addCustomers( List<Customer> newCustomers )
{
  [...]
}

But if I use only one of the two signatures, I can't get the service to accept both a single resource element as well as an array of elements:

If I use signature 1), I get a ClassCastException like this: Customer cannot be cast to java.util.Collection when the client sends an array instead of a single element.

If I use signature 2), I get an IllegalArgumentException: argument type mismatch when the client sends a single element instead of an array.

I've found this question that shows me that in Jackson, there's a feature ACCEPT_SINGLE_VALUE_AS_ARRAY that seems to deal with exactly this problem. I can't find an equivalent for that in MOXy, is there a way to achieve this?

Community
  • 1
  • 1
Hein Blöd
  • 1,553
  • 1
  • 18
  • 25

0 Answers0