@XmlElementWrapper用于使用JAX-WS的Web方法

时间:2011-10-23 23:45:07

标签: java xml web-services jax-ws

我有一个像下面这样的网络服务,它包含一个web方法,它将返回一个对象列表:

@WebService(name = "ClubMembershipPortType", serviceName = "ClubMembershipService", portName = "ClubMembershipSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubMembershipWS {
  @WebMethod(operationName = "findClubMembershipsByClubId", action = "urn:findClubMembershipsByClubId")
  @WebResult(name = "club_membership")
  public List<ClubMembership> findClubMembershipsByClubId(@XmlElement(required=true)
                                                        @WebParam(name = "club_id") String clubId, 
                                                        @WebParam(name = "status") StatusEnum status)
  ...
  ...
  }
}

我为api请求得到的回复如下:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:findClubMembersByClubIdResponse xmlns:ns2="http://club.com/api/ws">
         <club_membership>
            ...
         </club_membership>
         <club_membership>
            ...
         </club_membership>
      </ns2:findClubMembersByClubIdResponse>
   </S:Body>
</S:Envelope>

问题是如何使用@XmlElementWrapper(或其他方式?)来做出如下所示的回复?

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:findClubMembersByClubIdResponse xmlns:ns2="http://club.com/api/ws">
         <club_membership_list>
             <club_membership>
               ...
            </club_membership>
            <club_membership>
               ...
            </club_membership>
         </club_membership_list>
      </ns2:findClubMembersByClubIdResponse>
   </S:Body>
</S:Envelope>

2 个答案:

答案 0 :(得分:2)

你试过这个吗?

@XmlElementWrapper(name="club_membership_list", required=true)  
@XmlElement(name="club_membership", required=true) 
public List<ClubMembership> findClubMembershipsByClubId(@WebParam(name = "club_id") String clubId, 
@WebParam(name = "status") StatusEnum status)

答案 1 :(得分:0)

使用以下方法注释您的方法:

@WebResult(name="club_membership_list", targetNamespace = "http://club.com/api/ws")