users@jersey.java.net

[Jersey] Re: JAXB problem in Jersey responses

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 11 Oct 2011 12:12:45 +0200

Hi Rafael,

Would you be able to send a small reproducible test case?
 From top of my head, i do not see why that should fail
this way.

Otherwise a small comment to your design. Do you really
need the HTTPResponse wrapper? Status code goes
to the real HTTP response, and you can control that
by returning a Response (see [1]) instance.
If that is not absolutely mandatory, i would suggest
you drop this wrapper and use Invoice/List<Invoice>
directly as the entity objects.

~Jakub

[1]http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/core/Response.html

On 4.10.2011 23:35, Rafael Nunes Verger wrote:
> Hi all, here's the problem:
>
> @XmlRootElement
> @XmlSeeAlso({
> Client.class,
> Invoice.class
> })
> public class HTTPResponse {
> private int code;
> private Object data;
>
> ... getters and setters ..
> }
>
>
> @Path("invoice")
> public class InvoiceService {
> ... others methods ...
> /**
> * Return a list of invoices created for the client.
> *
> * @param email, client's email
> * @return
> */
> @GET
> @Path("{email:[^@]+@[^\\.]+(\\.[^\\.]+)+}")
> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
> public HTTPResponse list(@PathParam("email") String email){
> HTTPResponse response = new HTTPResponse();
> try{
> Invoice[] list = InvoiceDAO.list(email);
> response.setCode(ServiceStatus.SUCCESS);
> response.setData(list);
> } catch (NoResultException e) {
> response.setCode(ServiceStatus.NOT_FOUND);
> response.setData("No invoices found for client with email: " +
> email);
> }
> return response;
> }
> ... more code ...
> }
>
>
> So, in other methods that a simple Invoice object was set with
> HTTPResponse.setData all works, but in method List, when I need to
> return a list of invoices, the JAXB marshalling return an error that I
> couldn't fix:
> [javax.xml.bind.JAXBException: class
> [Lcom.mystore.payment.entity.Invoice; nor any of its super class is
> known to this context.]
>
>
> --
> Rafael Nunes Verger -- Software Developer
>