How to set custom ValidationEventHandler on JAXB unmarshaller when using annotations

前端 未结 1 1277
庸人自扰
庸人自扰 2020-12-19 09:17

We’re using JAX-WS in combination with JAXB to receive and parse XML web service calls. It’s all annotation-based, i.e. we never get hold of the JAXBContext in our code. I

相关标签:
1条回答
  • 2020-12-19 09:32

    I have been struggling with this issue during the last week and finally i have managed a working solution. The trick is that JAXB looks for the methods beforeUnmarshal and afterUnmarshal in the object annotated with @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);
      }
    

    Using this 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;
          }
    }
    

    }

    And this is the method getSchemaFromContext created in your Utility class:

      @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);
      }
    
    0 讨论(0)
提交回复
热议问题