Equivalent to Jackson's ACCEPT_SINGLE_VALUE_AS_ARRAY in MOXy?

空扰寡人 提交于 2019-12-11 06:24:27

问题


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?

来源:https://stackoverflow.com/questions/27247253/equivalent-to-jacksons-accept-single-value-as-array-in-moxy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!