Sono stato alle prese con questo problema durante l'ultima settimana e finalmente ho gestito una soluzione funzionante. Il trucco è che JAXB cerca i metodi beforeUnmarshal e afterUnmarshal nell'oggetto annotato con @XmlRootElement.
..
@XmlRootElement(name="MSEPObtenerPolizaFechaDTO")
@XmlAccessorType(XmlAccessType.FIELD)
public class MSEPObtenerPolizaFechaDTO implements Serializable {
..
public void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws JAXBException, IOException, SAXException {
unmarshaller.setSchema(Utils.getSchemaFromContext(this.getClass()));
unmarshaller.setEventHandler(new CustomEventHandler());
}
public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws JAXBException {
unmarshaller.setSchema(null);
unmarshaller.setEventHandler(null);
}
Usando questo ValidationEventHandler:
public class CustomEventHandler implements ValidationEventHandler{
@Override
public boolean handleEvent(ValidationEvent event) {
if (event.getSeverity() == event.ERROR ||
event.getSeverity() == event.FATAL_ERROR)
{
ValidationEventLocator locator = event.getLocator();
throw new RuntimeException(event.getMessage(), event.getLinkedException());
}
return true;
}
}
}
e questo è il metodo getSchemaFromContext creati nella classe di utilità:
@SuppressWarnings("unchecked")
public static Schema getSchemaFromContext(Class clazz) throws JAXBException, IOException, SAXException{
JAXBContext jc = JAXBContext.newInstance(clazz);
final List<ByteArrayOutputStream> outs = new ArrayList<ByteArrayOutputStream>();
jc.generateSchema(new SchemaOutputResolver(){
@Override
public Result createOutput(String namespaceUri,
String suggestedFileName) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
outs.add(out);
StreamResult streamResult = new StreamResult(out);
streamResult.setSystemId("");
return streamResult;
}
});
StreamSource[] sources = new StreamSource[outs.size()];
for (int i = 0; i < outs.size(); i++) {
ByteArrayOutputStream out = outs.get(i);
sources[i] = new StreamSource(new ByteArrayInputStream(out.toByteArray()), "");
}
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
return sf.newSchema(sources);
}
@BlaiseDoughan speravo ho potuto ottenere il vostro opinione su se ciò che stiamo tentando è anche possibile. qualche idea? – bobsmells
Chiunque? Sembra che questi "buchi" nel supporto delle annotazioni per alcune caratteristiche (in combinazione con l'aspetto normalmente buono delle annotazioni che portano via i dettagli di bootstrap) significano che non si possono realmente usare annotazioni se è necessario quelle caratteristiche. Devo essere in errore, ne sono sicuro. – bobsmells
Hai ottenuto qualche informazione su questo problema? Lottando con lo stesso problema qui. – krtek