java - How to customize spring webservice's PayloadValidatingInterceptor response message? -
i have java web service application built jaxb , spring webservice.
i have complex type in xsd this:
... <complextype name="getrecordsrequest"> <sequence> <element name="maxrecords" type="int" maxoccurs="1" minoccurs="1"/> </sequence> </complextype> ...
using xjc, had jaxb class generated 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 make sure user can't input besides integer 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 entered 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 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: '' not valid value 'integer'.</spring-ws:validationerror> <spring-ws:validationerror xmlns:spring-ws="http://springframework.org/spring-ws">cvc-type.3.1.3: value '' of element 'cis:maxrecords' not valid.</spring-ws:validationerror> </detail> </soap-env:fault> </soap-env:body> </soap-env:envelope>
you can see result 2 lines cryptic message 1 field. can make response message more prettier making 1 line? there way customize validation error response message?
you can customize validation error response using methods of abstractvalidatinginterceptor (payloadvalidatinginterceptor implementation of 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 soapfault here } } }
Comments
Post a Comment