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

发布于 2024-12-11 09:09:36 字数 1821 浏览 0 评论 0原文

我有一个如下所示的 Web 服务,它包含一个将返回对象列表的 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>

I have a web service like below, it contains a web method which will return a list of objects:

@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)
  ...
  ...
  }
}

The response I got for the api request is like below:

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

The question is how to use @XmlElementWrapper (or other way?) to make the response like below?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍风燃霜 2024-12-18 09:09:36

你尝试过这个吗?

@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)

Did you try this?

@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)
铁憨憨 2024-12-18 09:09:36

用以下方式注释你的方法:

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

Annotate your method with:

@WebResult(name="club_membership_list", targetNamespace = "http://club.com/api/ws")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文