Server-Side XML Validation with CXF Webservice

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 whether the insert was successful. I’d like to rely on XML validation to enforce a number of constraints on the request.

So, my question. How do I return detailed validation errors to a client of my service? I’ve turned validation on server-side by configuring my endpoint.

<jaxws:endpoint id="someEndpoint" implementor="#someImpl" address="/impl">
    <jaxws:properties>
        <!-- This entry should- ideally- enable JAXB validation
        on the server-side of our web service. -->
        <entry key="schema-validation-enabled" value="true" />
    </jaxws:properties>
</jaxws:endpoint>

I’ve explored using interceptors (eg BareInInterceptor) on the server, and somehow catching SAXParseExceptions to wrap them and send them along to the client. This approach seems a bit complicated, but I need to somehow give clients a line number if their XML is invalid. Should I go with interceptors to expose the exceptions?

I’m not very experienced with this technology stack, and just getting in to web services- any pointers you guys can give me would be really appreciated.

Weird validation error in beans.xml Spring CXF

Currently working on CXF with Spring. It returns this warning: 10:40:38,492 WARN [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-2) Ignored XML validation warning

Allow CXF webservice receive XML as string – CDATA

I am tasked to develop a webservice proxy which implement the same wsdl as the target webservice (original webservice). My problem is, the String_1 field contains xml string, e.g. <String_1>xm

Mule CXF consuming webservice

i am creating one webservice in mule using cxf:jaxws-service. This is the url :http://localhost:65042/InsertDocService/InsertDoc, i am ablie to generate WSDL file, but i want to consume this service i

Hiding the Restful service from Webservice list(Apache CXF)

I am working with Apache CXF Framework in which I have specified the Restful as well as the Soap Webservice in cxf-servlet.xml,After Deploying the project and requesting the service list by url as htt

CXF validation custom handler

I’m creating a webservice with cxf in the following way: <cxf:cxfEndpoint id=XXXEndpoint serviceClass=com.Sth address=${webservices.url}/XXX wsdlURL=${wsdl.address}/services/XXX.wsdl endpoin

Bean changes not reflect on CXF Restful webservice

I have implemented a CXF restful webservice. There is some object which does not have default constructor are throwing Error while calling webservice. Error: WARNING: javax.xml.bind.MarshalException

Camel CXF consumer, webservice method has no response

I’m trying to consume a webservice from camel using the cxf component, like this: <cxf:cxfEndpoint id=webservice address=http://webservice.url.com/webservice serviceClass=com.url.webservice.My

WebService Error In CXF

I created a wsdl in eclipse and I generated classes using Apache CXF. And after that I integrated that service in Jetty Server. I implement server side code. I use SOAP UI tool to test the implementat

CXF Webservice client breaks when providing wsdl

I’ve created a webservice client using cxf-codegen maven plugin and then instantiate the service like this: JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setEndpointName(SERVIC

MVC3 DataAnnotations ServerSide Validation

So im new to MVC3 and expirimenting with DataAnnotations for the validation. All is working fine clientside, but how do I get the serverside version working? If I disable Javascript then there are no

Answers

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:

<jaxws:endpoint id="someEndpoint" implementor="#someImpl" address="/impl">
    <jaxws:properties>
        <entry key="schema-validation-enabled" value="true" />
        <entry key="jaxb-validation-event-handler">
            <bean class="example.MyValidationEventHandler" />
        </entry>
    </jaxws:properties>
</jaxws:endpoint>

Then you will get SOAP faults that look like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Client</faultcode>
            <faultstring>Unmarshalling Error: Not a number: xyz [line: 6]</faultstring>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

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.