我有一个用 jaxb 和 spring webservice 构建的 java web 服务应用程序。
我在 xsd 中有一个复杂的类型,如下所示:
...
<complexType name="GetRecordsRequest">
<sequence>
<element name="maxRecords" type="int" maxOccurs="1" minOccurs="1"/>
</sequence>
</complexType>
...
使用xjc,我得到了从 xsd 生成的 jaxb 类:
public class GetRecordsRequest {
protected int maxRecords;
public int getMaxRecords() {
return maxRecords;
}
public void setMaxRecords(int value) {
this.maxRecords = value;
}
}
我在 spring context.xml 中使用了 PayloadValidatingInterceptor来确保用户不能为 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>
当我在 Soap UI 中输入这个肥皂请求 xml 时:
<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>
我得到的回复信息是:
<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>
您可以看到结果是仅针对一个字段的两行神秘消息。我可以通过只写一行来使响应消息更漂亮吗?有没有办法自定义验证错误响应消息?