开发者

JBoss AS7 + RestEasy : How to enable a custom MessageBodyReader using @Provider did nothing

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")
@Pro开发者_如何转开发vider
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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜