Server-Side XML Validation with CXF Webservice

前端 未结 1 1256
长情又很酷
长情又很酷 2020-12-01 02:29

I\'m working on an Apache CXF webservice (using JAX-WS, over SOAP). The service itself is pretty simple: receive a request, insert the request into a database, and return w

1条回答
  •  春和景丽
    2020-12-01 02:45

    You can override validation error messages, inserting a line number, by using a custom ValidationEventHandler:

    package example;
    
    import javax.xml.bind.ValidationEvent;
    import javax.xml.bind.helpers.DefaultValidationEventHandler;
    
    public class MyValidationEventHandler extends DefaultValidationEventHandler {    
        @Override
        public boolean handleEvent(ValidationEvent event) {
            if (event.getSeverity() == ValidationEvent.WARNING) {
                return super.handleEvent(event);
            } else {
                throw new RuntimeException(event.getMessage()
                    + " [line:"+event.getLocator().getLineNumber()+"]");
            }
        }
    }
    

    If you configure your endpoint to use this handler:

    
        
            
            
                
            
        
    
    

    Then you will get SOAP faults that look like this:

    
        
            
                soap:Client
                Unmarshalling Error: Not a number: xyz [line: 6]
            
        
    
    

    The jaxb-validation-event-handler property was only added to CXF pretty recently, so you need to make sure you're using the latest version - I tested this with 2.2.5.

    0 讨论(0)
提交回复
热议问题