I have a wierd problem. I'm using @Provider to annote my Mapper Exception and it's work fine, but when I'm using it to annote the class below it won't work at all.
@Consumes("application/x-java-serialized-object")
@Provider
public class JAXBSpecificMarshaller implements MessageBodyReader
{
@PersistenceContext(unitName = "primary", type = PersistenceContextType.EXTENDED)
private EntityManager em;
@Override
public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType)
{
return type.isAnnotationPresent(XmlRootElement.class);
}
@Override
public Object readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException
{
try
{
// DataAdapter dataAdapter = new DataAdapter(em);
//unmarshaller.setAdapter(dataAdapter);
System.out.println(type.getName());
JAXBContext ctx = JAXBContext.newInstance(type);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
return unmarshaller.unmarshal(entityStream);
}
catch ( JAXBException ex )
{
throw new RuntimeException(ex);
}
}
}
My main reason is to be able to use specific adapter to retrieve an object by passing its id in the input xml. I followed this Serialize a JAXB object via its ID? . But to initialize the adapter with my enitymanger I was told to use MessageBodyReader to do so.
Thank you for your help.