Is it possible to enable schema validation for inb

2019-04-14 03:49发布

问题:

I have created a web service endpoint using Apache CXF 2.5.2, but I am having some issues with schema validation and MTOM interacting. If I enable MTOM and schema validation I must use the base64Binary type directly, however I am trying to conform to a fixed spec where the MTOM field also has a "contentType" attribute.

<jaxws:properties>
    <entry key="mtom-enabled" value="true"/>
    <entry key="schema-validation-enabled" value="true"/>
</jaxws:properties>

Is it possible to only enable schema validation for inbound or outbound messages? For example something like:

<entry key="schema-validation-enabled" value="inbound"/>

Alternatively is there an alternate way of achieving this, such as overriding the outbound message validation?

Thanks.

回答1:

Since Apache CXF 3.0 this is sort of possible. You can't disable the validation on an in/outbound basis, but you can ignore the validation errors selectively (so you're still getting the performance hit).

You configure reader (inbound) & writer (outbound) validation event handlers in the CXF configuration.

<jaxws:properties>
    <!-- Validation of the SOAP Message--> 
    <entry key="schema-validation-enabled" value="true" />

    <entry key="jaxb-reader-validation-event-handler">
        <bean class="com.example.cxf.InboundValidationEventHandler" />
    </entry>

    <entry key="jaxb-writer-validation-event-handler">
        <bean class="com.example.cxf.OutboundValidationEventHandler" />
    </entry>
</jaxws:properties>

Create the ValidationEventHandlers like this and return true. Returning true informs CXF to ignore a single validation error and continue validation.

package com.example.cxf;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

public class InboundValidationEventHandler implements ValidationEventHandler {

    public boolean handleEvent(ValidationEvent event) {
        String message = event.getMessage();
        Throwable t = event.getLinkedException();

        System.out.println("Ignoring Inbound Validation EVENT : " +  message);

        // ignore
        return true;
    }
}