我有一个用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;
}
}
现在,问题是,如果我从SoapUI应用程序的soap请求xml中为maxRecords输入空值,如下所示:
<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>
我在网络服务端点类方法中得到的maxRecords值为0。我希望应用程序将引发错误或异常,因为我在xsd中设置了minOccurs =“ 1”,我认为这意味着强制性。
@PayloadRoot(namespace="http://www.test.com/ns1", localPart = "GetRecordsRequest")
public JAXBElement<GetRecordsResponse> GetRecordsRequest(JAXBElement<GetRecordsRequest> jaxbGetListMessage){
GetRecordsRequest request = jaxbGetListMessage.getValue();
System.out.println(request.getMaxRecords()); // print 0 value
...
}
我什至在xsd中将minOccurs更改为0,因此类型变为Integer,但是maxRecords值仍为0,我希望它将为null。
我知道的唯一方法是将maxRecords的类型更改为字符串或令牌,但是如果有其他解决方案仍保持其整数类型,则我更愿意。
因此,当我在soap xml中输入空值时,如何使maxRecords值为null或发生异常?
注意:我通过删除不相关的部分简化了上面的代码/ xml,以使代码更易于阅读。如果您发现了语法错误,请在注释部分中告知我,因为我手动输入了大部分代码。
最佳答案
我遵循其他人的建议使用PayloadValidatingInterceptor,并在应用程序中添加了类似的内容。上下文XML,它的工作原理是:
<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>
感谢所有建议使用PayloadValidatingInterceptor的人。