JBoss AS7 + RestEasy:如何使用 @Provider 启用自定义 MessageBodyReader 没有执行任何操作

发布于 2024-12-07 03:36:48 字数 1454 浏览 0 评论 0原文

我有一个奇怪的问题。我使用 @Provider 来注释我的 Mapper Exception 并且它工作正常,但是当我使用它来注释下面的类时它根本不起作用。

@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);
    }
  }


}

我的主要原因是能够使用特定的适配器通过在输入 xml 中传递对象的 id 来检索对象。我按照这个 Serialize a JAXB object via its ID? .但要使用我的 enitymanger 初始化适配器,我被告知要使用 MessageBodyReader 来执行此操作。

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏雪无痕 2024-12-14 03:36:48

您能否提供一些有关您要部署到哪个应用程序服务器以及您正在使用什么 JAX-RS 实现的上下文?

我在 JBoss AS 7 上的 RESTeasy 上遇到了类似的问题,试图为某些 JAXB 带注释的类实现 @Produces @Provider,但 RESTeasy 提供的 JAXB 编组器提供程序始终优先,而我的编组器从未被执行。

我的解决方案是编写自定义 JAXBContextFinder、ContextResolver 和 JAXBContext 的实现。我使用resteasy-jettison-provider源代码作为实现我自己的处理程序的秘诀。 http://docs.jboss.org/resteasy /docs/2.0.0.GA/userguide/html/Built_in_JAXB_providers.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文