@WebParam的@XmlElement(必需=true)不起作用

2022-09-03 15:27:12

我正在使用 JAX-WS 构建 Web 服务。我有一个奇怪的问题,即注释在某些类中起作用,但在其他一些类中不起作用。@XmlElement(required=true)@WebParam@WebService

我在这两个类中有非常相似的代码。什么可能导致此问题?参数类型还是实体类?@WebService

编辑:添加示例代码

我有两个网络服务:

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

@WebService(name = "ClubPortType", serviceName = "ClubService", portName = "ClubSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubWS {
@WebMethod(operationName = "findClubByClubId", action = "urn:findClubByClubId")
    @WebResult(name = "club")
    public Club findClubByClubId(@XmlElement(required=true)
                                @WebParam(name = "club_id") String clubId) {
...
}}

为第一个 Web 方法生成的架构是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:findClubMembersByClubId>
         <club_id>?</club_id>
         <!--Optional:-->
         <status>?</status>
      </ws:findClubMembersByClubId>
   </soapenv:Body>
</soapenv:Envelope>

为第二个 Web 方法生成的架构是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://club.com/api/ws">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:findClubByClubId>
         <!--Optional:-->
         <club_id>?</club_id>
      </ws:findClubByClubId>
   </soapenv:Body>
</soapenv:Envelope>

所以第一个工作正常,第二个不起作用。这怎么可能?:(


答案 1

添加后解决了我的类似问题。使用 CXF 2.7.9。没有尝试放在第一位,可以这么简单吗?@XmlElement(required=true,nillable=false)@WebParam@XmlElement


答案 2

我有同样的问题。我在使用单独的类作为服务方法的参数时找到了解决方案。

例如:

@XmlType(name="SampleRequestType", propOrder={"title", "ref"})
public class SampleRequest {
    @XmlElement(name="title", required=false)
    private String title;
    @XmlElement(name="ref", required=true)
    private String ref;
    ...

网络方法

@WebMethod
public String sampleMethod(@WebParam(name = "params") SampleRequest params) {

也许这会有所帮助


推荐