0

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.

Community
  • 1
  • 1
Ghostwan
  • 263
  • 1
  • 4
  • 11
  • Do you know that your `MessageBodyReader` is not deploying? Is it possible the `isReadable` method is always returning false? – bdoughan Sep 28 '11 at 16:46
  • But even if I send always true in isReadable it's won't work. with the debugger I find that my Entity Class that I want to deserialize is not a parameterizedtype so, the marshaller don't use it and it prefere a CollectionProvider – Ghostwan Sep 29 '11 at 12:48

1 Answers1

0

Can you provide some context on what application server you are deploying to and what JAX-RS implementation you are using?

I had a similar problem with RESTeasy on JBoss AS 7 trying to implement a @Produces @Provider for some JAXB annotated classes, but the provided JAXB marshaller provider from RESTeasy always took precedence, and my marshaller never got executed.

My solution was to write implementations for custom JAXBContextFinder, ContextResolver and JAXBContext. I used resteasy-jettison-provider source code as a recipe for implementing my own handlers. http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/Built_in_JAXB_providers.html

hovenko
  • 713
  • 7
  • 15
  • I'm deploying a war on Jboss 7. Thing is, if I use the source from http://stackoverflow.com/questions/7278406/serialize-a-jaxb-object-via-its-id/7285517#7285517 It work on my project, but when I change the type use on the the MessageBodyWriter by my type it won't worker either. – Ghostwan Sep 29 '11 at 12:53
  • I think I have the same problem as you, because I saw the provider but it won't choose it. Do you have any source for JAXBContextFinder, ContextResolver, JAXBContext. Thanks – Ghostwan Sep 29 '11 at 12:59
  • Sorry for the late response, and you probably found it yourself. For reference: http://grepcode.com/file/repository.jboss.org/nexus/content/repositories/releases/org.jboss.resteasy/resteasy-jettison-provider/1.2.1.GA/org/jboss/resteasy/plugins/providers/jaxb/json/JsonJAXBContextFinder.java?av=f – hovenko Oct 19 '11 at 12:02
  • FYI: It might be that this is related to this issue which is fixed in RestEasy 2.3.3.Final https://issues.jboss.org/browse/RESTEASY-555 I found it after having a similar problem using 2.3.1.GA. – Mattias Holmqvist May 22 '12 at 14:13