You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by David Hay <da...@enstratius.com> on 2013/09/09 17:35:21 UTC

Mapping POSTed JSON Enum parameter which implements an Interface

Hi,

I have a class with 2 enum properties - a concrete one (A), and one that
implements an interface (B) and has multiple implementations, which are
possible values for the selected value of A.  If A has the value 1, the B
will be of type Enum1, which has the possible values for type 1.

When I POST to a REST endpoint to create this object, CXF has problems
because of the Interface.

What's the best way to add something that will allow JSON posted to be
mapped to the right enum type for the 2nd Enum?

Re: Mapping POSTed JSON Enum parameter which implements an Interface

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 09/09/13 17:11, David Hay wrote:
> Hi Sergey,
>
> Digging a little deeper, I suspect that I have to do some kind of custom
> code here...  When the JSON is read, the right type of Enum B needs to
> be selected, which is further complicated by the fact that enums that
> implement B can share the same text string too.
>
> Is it best to provide my only MessageBodyReader, or is there some kind
> of type mapper I can add?
>

I guess providing a custom MBR is the way to go, may be you extend 
JSONProvider to simplify it a bit

Cheers, Sergey

>
>
>
> On Mon, Sep 9, 2013 at 11:44 AM, Sergey Beryozkin <sberyozkin@gmail.com
> <ma...@gmail.com>> wrote:
>
>     Hi
>
>     On 09/09/13 16:35, David Hay wrote:
>
>         Hi,
>
>         I have a class with 2 enum properties - a concrete one (A), and
>         one that
>         implements an interface (B) and has multiple implementations,
>         which are
>         possible values for the selected value of A.  If A has the value
>         1, the B
>         will be of type Enum1, which has the possible values for type 1.
>
>         When I POST to a REST endpoint to create this object, CXF has
>         problems
>         because of the Interface.
>
>         What's the best way to add something that will allow JSON posted
>         to be
>         mapped to the right enum type for the 2nd Enum?
>
>     Can you please try CXF 2.7.7-SNAPSHOT (or CXF 2.6.10-SNAPSHOT) and
>     see if it handles it better ?
>     Or show here how the code looks like
>
>     Cheers, Sergey
>
>

Re: Mapping POSTed JSON Enum parameter which implements an Interface

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 09/09/13 16:35, David Hay wrote:
> Hi,
>
> I have a class with 2 enum properties - a concrete one (A), and one that
> implements an interface (B) and has multiple implementations, which are
> possible values for the selected value of A.  If A has the value 1, the B
> will be of type Enum1, which has the possible values for type 1.
>
> When I POST to a REST endpoint to create this object, CXF has problems
> because of the Interface.
>
> What's the best way to add something that will allow JSON posted to be
> mapped to the right enum type for the 2nd Enum?
>
Can you please try CXF 2.7.7-SNAPSHOT (or CXF 2.6.10-SNAPSHOT) and see 
if it handles it better ?
Or show here how the code looks like

Cheers, Sergey

RE: Mapping POSTed JSON Enum parameter which implements an Interface

Posted by "KARR, DAVID" <dk...@att.com>.
> -----Original Message-----
> From: David Hay [mailto:david.hay@enstratius.com]
> Sent: Monday, September 09, 2013 8:35 AM
> To: users@cxf.apache.org
> Subject: Mapping POSTed JSON Enum parameter which implements an Interface
> 
> Hi,
> 
> I have a class with 2 enum properties - a concrete one (A), and one that
> implements an interface (B) and has multiple implementations, which are
> possible values for the selected value of A.  If A has the value 1, the B
> will be of type Enum1, which has the possible values for type 1.
> 
> When I POST to a REST endpoint to create this object, CXF has problems
> because of the Interface.
> 
> What's the best way to add something that will allow JSON posted to be
> mapped to the right enum type for the 2nd Enum?

I would certainly follow Sergey's suggestion, but I would point out a useful strategy for debugging problems like this.

CXF not only translates JSON to Java objects, it can go the other direction.  If you don't understand why your JSON didn't translate properly to your Java object, then try implementing a service that goes in the other direction and look at what JSON it produces, and then compare that with the JSON you were using.  You'll find that you were either doing something wrong in your JSON, or you found a problem that needs to be dealt with.  In any case, you'll have narrowed down the problem.