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 来检索对象。我通过它的 ID 跟随这个序列化一个 JAXB 对象?. 但是要使用我的 enitymanger 初始化适配器,我被告知要使用 MessageBodyReader 来这样做。

感谢您的帮助。

4

1 回答 1

0

您能否提供一些有关您正在部署到什么应用程序服务器以及您正在使用什么 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

于 2011-09-28T17:17:10.497 回答