3

I have a java web service application built with jaxb and spring webservice.

I have a complex type in a xsd like this:

...

<complexType name="GetRecordsRequest">
    <sequence>
        <element name="maxRecords" type="int" maxOccurs="1" minOccurs="1"/>
    </sequence>
</complexType>

...

Using xjc, I had the jaxb class generated from xsd:

public class GetRecordsRequest {
    protected int maxRecords;

    public int getMaxRecords() {
        return maxRecords;
    }

    public void setMaxRecords(int value) {
        this.maxRecords = value;
    }
}

I used PayloadValidatingInterceptor in spring context.xml to make sure user can't input anything besides integer for maxRecords:

<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
    <property name="interceptors">
        <list>
            <ref local="validatingInterceptor" />
        </list>
    </property>
</bean>

<bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
    <property name="schema" value="/WEB-INF/schemas/webservice.xsd" />
    <property name="validateRequest" value="true" />
    <property name="validateResponse" value="true" />
</bean>

When I entered this soap request xml in Soap UI:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.test.com/ns1">
   <soapenv:Header/>
   <soapenv:Body>
      <ns1:GetRecordsRequest>
         <ns1:maxRecords></ns1:maxRecords>
      </ns1:GetRecordsRequest>
   </soapenv:Body>
</soapenv:Envelope>

The response message I got is:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Client</faultcode>
         <faultstring xml:lang="en">Validation error</faultstring>
         <detail>
            <spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.</spring-ws:ValidationError>
            <spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-type.3.1.3: The value '' of element 'cis:maxRecords' is not valid.</spring-ws:ValidationError>
         </detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can see the result is two lines cryptic message for just one field. Can I make the response message more prettier by making just one line? Is there a way to customize the validation error response message?

2 Answers 2

3

You can customize the validation error message by extending PayloadValidatingInterceptor and overriding handleRequestValidationErrors. We can set the custom error message in the body of messageContext.

1) Instead of the SOAP Fault for request validation errors, you can return custom xml response with validation error message.

2) SAXParseException[] errors contains the request validation errors. You can choose to return only one error in response. (or) For some predefined errors, you can return custom error message rather than the one returned in SAXParseException.

/**
* The Class CustomValidatingInterceptor.
*/
public class CustomValidatingInterceptor extends PayloadValidatingInterceptor{

/* (non-Javadoc)
 * @see org.springframework.ws.soap.server.endpoint.interceptor.AbstractFaultCreatingValidatingInterceptor#handleRequestValidationErrors(org.springframework.ws.context.MessageContext, org.xml.sax.SAXParseException[])
 */
@Override
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors) throws TransformerException {
    JAXBContext jaxbContext;
    StringWriter stringWriter = new StringWriter();
    ResponseTransactionDetail transactionDetail = null;

    for (SAXParseException error : errors) {
        logger.debug("XML validation error on request: " + error.getMessage());
    }
    if (messageContext.getResponse() instanceof SoapMessage) {

        /**
         * Get SOAP response body in message context (SOAP Fault)
         */

        SaajSoapMessage soapMessage = (SaajSoapMessage)messageContext.getResponse();
        SoapBody body = soapMessage.getSoapBody();

        // marshal custom error response to stringWriter

        /**
        * Transform body
        */
        Source source = new StreamSource(new StringReader(stringWriter.toString()));
        identityTransform.transform(source, body.getPayloadResult());
        stringWriter.close();
    }
    return false;
}
2

You can customize the validation error response by using the methods of the AbstractValidatingInterceptor (PayloadValidatingInterceptor is an implementation of this abstract class) namely:

  • setDetailElementName(QName detailElementName)
  • setFaultStringOrReason(String faultStringOrReason)

partial example:

public final class MyPayloadValidatingInterceptor
extends PayloadValidatingInterceptor {

    @Override
    protected Source getValidationRequestSource(WebServiceMessage webSerMessage_) {
        _source = webSerMessage_.getPayloadSource();
        validateSchema(_source);
        return _source;
    }

    private void validateSchema(Source source_) throws Exception {
        SchemaFactory _schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema _schema = _schemaFactory.newSchema(getSchemas()[0].getFile());

        Validator _validator = _schema.newValidator();
        DOMResult _result = new DOMResult();
        try {
            _validator.validate(source_, _result);
        } catch (SAXException _exception) {
            // modify your soapfault here                
        }
    }
}
7
  • So what should I do is to create a class that extends from AbstractValidatingInterceptor, then implement that two methods, correct? If that correct, how to get the element name from QName object? I don't see any element name field in the QName.
    – null
    Apr 2, 2013 at 5:55
  • No, you should use your PayloadValidatingInterceptor. First detect if there a fault, then modify the fault message using the two provided methods. Did you check the localpart from the QName ? It should be the element name. Apr 2, 2013 at 12:25
  • hi, I had created a class that extend PayloadValidatingInterceptor, then I also overrided setDetailElementName and setFaultStringOrReason method. I also put a breakpoint inside both methods in eclipse, but when I run the webservice, the breakpoint was not hit, so that means the two methods were not called at all. Is there something I miss? Do you want to see the code? I can send you in pastebin if you want to.
    – null
    Apr 4, 2013 at 9:09
  • @suud: No the default implementation of those methods are fine. You need to modify the getValidationRequestSource(WebServiceMessage request) method. In it, you need to call the super.getValidationRequestSource and detect if there is a fault. You probably need to do some kind of try catch. Apr 4, 2013 at 10:37
  • hi, I really can't figure out how to do this. Could you give an example code? What I need is just the informations about: 1) List/array of element names that has invalid value. 2) List/array of element values for each elements in point 1. 3) If possible too, the list/array of the causes of error for each elements in point 1 (such as invalid value because of wrong data type, empty value inserted for mandatory element, etc).
    – null
    Apr 5, 2013 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.