users@jersey.java.net

Re: [Jersey] JPA -> JAXB question

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 04 May 2009 10:57:41 +0200

Hi,

The instance returned by the resource method is the instance that is
marshaled by JAXB, essentially using the following:

http://java.sun.com/javase/6/docs/api/javax/xml/bind/Marshaller.html#marshal(java.lang.Object,%20java.io.OutputStream)

So you could do:

        @GET
        @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        @Path("/read/{id}")
        public Object read(@PathParam("id") String id) {
                return userFacade.read(new Long(id));
        }

and the same behavior will occur.

Paul.

On May 2, 2009, at 1:57 PM, Felipe Gaścho wrote:

> I have this method that reads from the database and expose the
> serialized entity on the RESTful interface of Jersey...
>
> everything works fine.. but I got a curious behavior.
>
> check out this method:
>
> @GET
> @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
> @Path("/read/{id}")
> public FpUser read(@PathParam("id") String id) {
> return userFacade.read(new Long(id));
> }
>
>
> this works fine.. BUT: I have this hierarchy:
>
> class FpUser
> class FpUserWritable extends FpUser
>
> when I run the above method, the returned XML represents a subclass
> FpUserWritable... not what I want..
>
> and then, I tried this method rewriting:
>
> @GET
> @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
> @Path("/read/{id}")
> public JAXBElement<FpUser> read(@PathParam("id") String id) {
> return new JAXBElement<FpUser>(FpUser.QUALIFIED_NAME, FpUser.class,
> userFacade.read(new Long(id)));
> }
>
>
> done, the returned type now is the superclass FpUser instead of its
> subclass...
>
>
> I am just trying to understand why the first code returned the full
> JPA entity -....
>
> other curiosity: if I return a Collection<FpUser> it works natively,
> without any adaptation needed.... :(
>
>
> --
>
> Please help to test this application:
> http://fgaucho.dyndns.org:8080/cejug-classifieds-richfaces
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>