【问题标题】:Making a web service request parameter a required field将 Web 服务请求参数设为必填字段
【发布时间】:2013-02-10 21:07:25
【问题描述】:

Jax-WS Web 服务的代码优先方法。

@WebService (serviceName = "MyInstallPhotoService")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class MyInstallPhotoWS {

    private MyInstallPhotoManager myInstallPhotoManager;

    @Resource
    WebServiceContext context;


    @WebMethod(operationName = "getMyInstallPhoto")
    @WebResult(name = "PhotoRetrievalResponse", partName = "PhotoRetrievalResponse")
    public MyInstallPhotoResponse getBadgePhoto(@WebParam(name = "BadgeNumber", partName = "BadgeNumber") String badgeNumber, @WebParam(name = "LastName", partName = "LastName") String lastName) {
        MyInstallPhotoResponse myInstallPhotoResponse = new MyInstallPhotoResponse();
        try {
            // more code here
        } catch (Exception e) {
          e.printStackTrace();
        }
        return myInstallPhotoResponse;
     }
}

在上面的代码中,MyInstallPhotoResponse 是在 xml 模式中定义的。 SoapUI 请求生成了类似这样的内容

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <rsw:getBadgePhoto>
         <!--Optional:-->
         <rsw:BadgeNumber>I180748-003</rsw:BadgeNumber>
         <!--Optional:-->
         <rsw:LastName>Jones</rsw:LastName>
      </rsw:getBadgePhoto>
   </soapenv:Body>
</soapenv:Envelope>

如何根据soapui 请求将BadgeNumber 和LastName 设为必填字段而不是可选字段。我试图将 badgeNumber 和 lastName 移动到对象 myinstallphotorequest(在架构中定义)并要求这两个参数。这是我收到的soapui请求。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myin="http://www.lexisnexis.com/myInstallPhotoService" xmlns:myin1="http://www.lexisnexis.com/schema/myInstallPhotoServiceTypes">
   <soapenv:Header/>
   <soapenv:Body>
      <myin:getMyInstallPhoto>
         <!--Optional:-->
         <myin:MyInstallPhotoRequest>
            <myin1:badgeNumber>?</myin1:badgeNumber>
            <myin1:lastName>?</myin1:lastName>
         </myin:MyInstallPhotoRequest>
      </myin:getMyInstallPhoto>
   </soapenv:Body>
</soapenv:Envelope>

我再次无法删除参数“MyInstallPhotoRequest”的 Optional。

【问题讨论】:

    标签: wsdl xsd jax-ws


    【解决方案1】:

    如果您检查 Web 服务的 WSDL 文件,则该参数应具有 minOccurs=0。这就是 SOAPUI 请求将可选 cmets 放在那里的原因。

    请使用@XmlElement(required=true) 注释您需要的WebParam。

    【讨论】:

    • 是的,WSDL 将其显示为 minOccures=0。此外,我无法将 @XmlElement(required=true) 添加到显示“此位置不允许使用注释 XmlElement”的 Web 参数中。我做了这个 public MyInstallPhotoResponse getBadgePhoto(@XmlElement(required=true) @WebPa.... 对吗?
    • 顺便说一句,我强烈推荐“合同优先”的方法,而不是“代码优先”的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2020-01-28
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多