Java 当SOAP XML中没有输入值时,如何使xsd中的整型元素值不变为零?

Java 当SOAP XML中没有输入值时,如何使xsd中的整型元素值不变为零?,java,soap,xsd,jaxb,spring-ws,Java,Soap,Xsd,Jaxb,Spring Ws,我有一个用jaxb和SpringWebService构建的JavaWeb服务应用程序 我在xsd中有一个复杂类型,如下所示: ... <complexType name="GetRecordsRequest"> <sequence> <element name="maxRecords" type="int" maxOccurs="1" minOccurs="1"/> </sequence> </complex

我有一个用jaxb和SpringWebService构建的JavaWeb服务应用程序

我在xsd中有一个复杂类型,如下所示:

...

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

...
<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>
现在的问题是,如果我从SoapUI应用程序在soap请求xml中为maxRecords输入空值,如下所示:

...

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

...
<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>

我在webservice端点类方法中得到了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

    ...
}
@PayloadRoot(命名空间=”http://www.test.com/ns1,localPart=“GetRecordsRequest”)
公共JAXBElement GetRecordsRequest(JAXBElement jaxbGetListMessage){
GetRecordsRequest请求=jaxbGetListMessage.getValue();
System.out.println(request.getMaxRecords());//打印0值
...
}
我甚至在xsd中将minOccurs更改为0,这样类型就变成了整数,但是maxRecords值仍然是0,我希望它是空的。

我知道的唯一解决方法是将maxRecords的类型更改为string或token,但如果有另一种解决方案仍然保持其整数类型,我更愿意这样做

那么,当我在soap xml中输入空值时,如何使maxRecords值为null或发生异常?


注意:我通过删除不相关的部分简化了上述代码/xml,使代码更易于阅读。如果您发现语法错误,请在注释部分告诉我,因为我手动键入了大部分代码。

int是Java中的一个原语,因此不能为null。您可以改为使用整数类型。整数可以为空,因此不会默认为0。

我按照其他人的建议使用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的人。

我的XSD看起来像这样,它生成了一个整数,而不是int。是的,它生成了一个整数,但是
System.out.println(request.getMaxRecords())仍然打印值0。我甚至通过在eclipse中的代码中设置断点来确认它。
getMaxRecords()
肯定有整数返回类型,但我仍然得到了0值。只有当我从soap xml中删除
时,我才得到空值。您是否实现了PayloadValidatingInterceptor?因为您不验证请求,所以在XSD中尝试什么都无所谓。确保使用像@Amine Inetriends这样的
PayloadValidatingInterceptor
用XSD验证请求