jaxbとSpringWebサービスで構築されたJavaWebサービスアプリケーションがあります。
私は次のような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アプリケーションからsoaprequestxmlに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>
Webサービスエンドポイントクラスメソッドで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の型を文字列またはトークンに変更することですが、整数型を維持したまま別の解決策がある場合は、それを好みました。
では、maxRecords値をnullにする方法、またはsoap xmlに空の値を入力したときに例外が発生した方法はありますか?
注:コードを読みやすくするために、関連のない部分を削除して、上記のcode/xmlを簡略化しました。構文のタイプミスを見つけた場合は、ほとんどのコードを手動で入力したので、コメントセクションで知らせてください。