我有一个奇怪的问题。我正在使用 @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 来这样做。
感谢您的帮助。