JAXB-WS - 使用@WebMethod强制字段

时间:2012-09-28 14:50:34

标签: jboss annotations jax-ws

我有一个@WebMethod调用

@WebMethod
public int cancelCampaign(String campaignId, String reason);

我想将campaignId字段标记为必填字段。不知道怎么做。

我正在使用JBOSS 7.1服务器。

2 个答案:

答案 0 :(得分:7)

我有类似的要求,从SoapUI我发现我得到了

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:bus="http://business.test.com/">
  <soapenv:Header/>
  <soapenv:Body>
     <!-- optional -->
     <bus:addItem>
        <bus:item>
           <id>?</id>
           <!-- optional -->
           <name>?</name>
        </bus:item>
        <!-- optional -->
        <itemType>?</itemType>
     </bus:addItem>
  </soapenv:Body>
</soapenv:Envelope>

而不是

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:bus="http://business.test.com/">
  <soapenv:Header/>
  <soapenv:Body>
     <bus:addItem>
        <bus:item>
           <id>?</id>
           <name>?</name>
        </bus:item>
        <itemType>?</itemType>
     </bus:addItem>
  </soapenv:Body>
</soapenv:Envelope>

JAX-WS Metro 2.0 RI的出路是用

注释每个参数
@XmlElement( required = true )

在我的情况下,我必须执行此操作以获取所有必需自定义类型的WebMethod参数和getter,如下所示:

在WebService中:

...
 @WebMethod( operationName = "getItems" )
   @WebResult( name = "item" )
   public List<Item> getItems(
     @WebParam( name = "itemType" ) @XmlElement( required = true ) String itemType );
...

在我的POJO课程中:

@XmlAccessorType(XmlAccessType.FIELD)
public class Item implements Serializable
{
   private static final long serialVersionUID = 1L;

   @XmlElement( required = true )
   private int               id;

   @XmlElement( required = true )
   private String            name;

   /**
    * Default constructor.
    */
   public Item() { }

   /**
    * @return the id
    *
    */       
   public int getId()
   {
      return id;
   }

   /* setter for id */

   /**
    * @return the name
    */
   public String getName()
   {
      return name;
   }

   /* setter for name */

}

答案 1 :(得分:4)

使用JAX-WS执行此操作的唯一方法是编写一些包装类,指定required=true注释上的XmlElement标志。您的请求元素应如下所示:

@XmlType(name="YourRequestType", propOrder={"campaignId", "reason"})
public class YourRequest {
    @XmlElement(name="campaignId", required=true)
    private String campaignId;
    @XmlElement(name="reason", required=false)
    private String reason;

    //Getters and setters        

}

您的网络方法应如下所示:

@WebMethod
public int cancelCampaign(@WebParam(name = "request") YourRequest request) {
   String campaignId = request.getCampaignId();

   return 0;
}

这会告诉JAXBminOccurs=1 XSD元素中生成campaignId