You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by DmitryM <ns...@mail.ru> on 2010/03/08 18:14:36 UTC

Jax-RS ExceptionMapper and data marshalling

Hello, all

Can anyone advise me on the following issue:

- I have 2 methods in the web interface:
  String addX() throws E;
  void updateX(String x) throws E;

- I also have an ExceptionMapper which maps server-side exceptions onto the
'500' response with a marshalled object using the call like this:

Response result = Response.status(resultStatus).entity(z).build();

where z is an instance of the 

@XmlRootElement(name="z")
public class Z { public String z1; public String z2; }

The matter is everything works perfectly fine on the updateX() call even in
case of E or a RuntimeException thrown inside it.
But when it comes to addX() (which returns java.lang.String type - not
annotated with @XmlRootElement) then I get a bizarre 

Error creating a JAXBContext using ObjectFactory ... package
'Z.package.goes.here' contain ObjectFactory.class or jaxb.index ...

When I change 
String addX() to 
StringWrapper addX()

where StringWrapper is the following 

@XmlRootElement(name="s")
StringWrapper { public String value; }

then everything works okay.
But this wrapping of standard Java types to make marshaller work properly
seems kinda lame.

Any help on how to use standard Java classes without extra wrapper classes
with ExceptionMapper using standard marshalers?

Thanks,
Dmitry
-- 
View this message in context: http://old.nabble.com/Jax-RS-ExceptionMapper-and-data-marshalling-tp27824429p27824429.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Jax-RS ExceptionMapper and data marshalling

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

sorry, I was not suggesting for you to go and find that thread :-) I think
there was a query to do with converting String[] into JSON, so we probably
thought doing something like
{strings:{string:"bar", string:"foo"}} would be ok...

similarly if it is just a String then
{string:"bar"}

would do.

Then again the question is what to do if it application/xml :
<string>bar</string>
or
<string value="bar"/>

etc

I'm a bit unenthusiastic about CXF JAXRS shipping another provider doing
such conversions given that it is very easy to have a custom provider doing
it.

cheers, Sergey

On Mon, Mar 8, 2010 at 6:29 PM, DmitryM <ns...@mail.ru> wrote:

>
> Sergey,
>
> Well, it kind of does. =)
> If you pointed me to that thread where you already discussed the issue...
> that would be excellent.
> Otherwise, I got the point.
>
> Thanks.
>
>
> Sergey Beryozkin-5 wrote:
> >
> > hope it helps
> >
>
> --
> View this message in context:
> http://old.nabble.com/Jax-RS-ExceptionMapper-and-data-marshalling-tp27824429p27825509.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>

Re: Jax-RS ExceptionMapper and data marshalling

Posted by DmitryM <ns...@mail.ru>.
Sergey,

Well, it kind of does. =)
If you pointed me to that thread where you already discussed the issue...
that would be excellent.
Otherwise, I got the point.

Thanks.


Sergey Beryozkin-5 wrote:
> 
> hope it helps
> 

-- 
View this message in context: http://old.nabble.com/Jax-RS-ExceptionMapper-and-data-marshalling-tp27824429p27825509.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Jax-RS ExceptionMapper and data marshalling

Posted by Sergey Beryozkin <sb...@gmail.com>.
OK, I see...The question is how to wrap a String value. I believe we
discussed it awhile back on the list...
Probably the simplest way to go forward is to create a custom JSON provider
which will extend the default one and in its writeTo it will check if it is
a primitive class and if yes then it will serialize the value as needed,
using some custom convention, otherwise it will delegate to the super
class...

hope it helps, Sergey

On Mon, Mar 8, 2010 at 5:44 PM, DmitryM <ns...@mail.ru> wrote:

>
> Sergey
>
> The matter is I'm already JSON provider and expect it to do the job for me.
> As I said, it worked perfectly fine for the second method (not having
> return
> type specified).
> And I would like to keep the methods in sync (as well as the result type,
> which is set to JSON).
>
> I was just wondering why it works okay without return type and doesn't work
> on standard Java types with return type specified.
>
> Dmitry
>
> Sergey Beryozkin-5 wrote:
> >
> > Response.status(resultStatus).type("text/plain").entity("error
> > message").build();
> >
> > rather than expecting primitive types being wrapped ? It can be done but
> I
> > haven't really had a chance to look into it - I do not see what real
> > benefits it will bring
> >
>
> --
> View this message in context:
> http://old.nabble.com/Jax-RS-ExceptionMapper-and-data-marshalling-tp27824429p27824861.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>

Re: Jax-RS ExceptionMapper and data marshalling

Posted by DmitryM <ns...@mail.ru>.
Sergey

The matter is I'm already JSON provider and expect it to do the job for me.
As I said, it worked perfectly fine for the second method (not having return
type specified).
And I would like to keep the methods in sync (as well as the result type,
which is set to JSON).

I was just wondering why it works okay without return type and doesn't work
on standard Java types with return type specified.

Dmitry

Sergey Beryozkin-5 wrote:
> 
> Response.status(resultStatus).type("text/plain").entity("error
> message").build();
> 
> rather than expecting primitive types being wrapped ? It can be done but I
> haven't really had a chance to look into it - I do not see what real
> benefits it will bring
> 

-- 
View this message in context: http://old.nabble.com/Jax-RS-ExceptionMapper-and-data-marshalling-tp27824429p27824861.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Jax-RS ExceptionMapper and data marshalling

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

perhaps you might want to do

Response.status(resultStatus).type("text/plain").entity("error
message").build();

rather than expecting primitive types being wrapped ? It can be done but I
haven't really had a chance to look into it - I do not see what real
benefits it will bring

cheers, Sergey

On Mon, Mar 8, 2010 at 5:14 PM, DmitryM <ns...@mail.ru> wrote:

>
> Hello, all
>
> Can anyone advise me on the following issue:
>
> - I have 2 methods in the web interface:
>  String addX() throws E;
>  void updateX(String x) throws E;
>
> - I also have an ExceptionMapper which maps server-side exceptions onto the
> '500' response with a marshalled object using the call like this:
>
> Response result = Response.status(resultStatus).entity(z).build();
>
> where z is an instance of the
>
> @XmlRootElement(name="z")
> public class Z { public String z1; public String z2; }
>
> The matter is everything works perfectly fine on the updateX() call even in
> case of E or a RuntimeException thrown inside it.
> But when it comes to addX() (which returns java.lang.String type - not
> annotated with @XmlRootElement) then I get a bizarre
>
> Error creating a JAXBContext using ObjectFactory ... package
> 'Z.package.goes.here' contain ObjectFactory.class or jaxb.index ...
>
> When I change
> String addX() to
> StringWrapper addX()
>
> where StringWrapper is the following
>
> @XmlRootElement(name="s")
> StringWrapper { public String value; }
>
> then everything works okay.
> But this wrapping of standard Java types to make marshaller work properly
> seems kinda lame.
>
> Any help on how to use standard Java classes without extra wrapper classes
> with ExceptionMapper using standard marshalers?
>
> Thanks,
> Dmitry
> --
> View this message in context:
> http://old.nabble.com/Jax-RS-ExceptionMapper-and-data-marshalling-tp27824429p27824429.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
>