Well, the basic reason is that many of the generic types get compiled away and 
thus are not able to be discovered via reflection.   In your case:

new CRUDResponse<Apple>(apple)

The "Apple" gets compiled away to just Object.   When we create the 
JAXBContext, Apple doesn't get added and thus it's now able to write it.   The 
normal way around it is to add @XmlSeeAlso annotations to places JAXB would 
look.   In your case, I THINK if you add an XmlSeeAlso annotation to the 
CRUDResponse that points at all the objects that it could hold, you should be 
OK.

Dan


On Mon August 17 2009 6:11:59 am Sam.Wang wrote:
> I found this issue today, but I don't understand why?
>
> 2009-8-17 18:05:09 org.apache.cxf.jaxrs.provider.AbstractJAXBProvider
> handleJAXBException
> Warning: javax.xml.bind.MarshalException
>  - with linked exception:
> [javax.xml.bind.JAXBException: class com.demo.Apple nor any of its super
> class is known to this context.]
>
> Following is my demo code:
>
> @XmlRootElement(name = "apple")
> public class Apple {
>
>       private int id = -1;
>       private String name;
>       private String color;
>       private int size;
>
>         set...
>         get...
> }
>
>
> @XmlRootElement(name = "response")
> public class CRUDResponse<T extends Object> {
>
>       private int total;
>       private int limit;
>       private int start;
>       private boolean success = true;
>       private T entity;
>       private List<T> entityList;
>
>         set...
>         get...
> }
>
>       public Response getApple(String id) throws Exception {
>               Apple apple = new 
> AppleServiceImpl().getApple(Integer.parseInt(id));
>               return Response.ok(new CRUDResponse<Apple>(apple)).build();
>       }

-- 
Daniel Kulp
[email protected]
http://www.dankulp.com/blog

Reply via email to