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

Shi*_*hao 15 java annotations jax-ws required

我正在使用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){
...
}}
Run Code Online (Sandbox Code Playgroud)

@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) {
...
}}
Run Code Online (Sandbox Code Playgroud)

生成的第一个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>
Run Code Online (Sandbox Code Playgroud)

为第二个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>
Run Code Online (Sandbox Code Playgroud)

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

小智 5

添加@XmlElement(required=true,nillable=false)@WebParam解决了我的类似问题。使用 CXF 2.7.9。没试过@XmlElement先放,会这么简单吗?


小智 4

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

例如

@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;
    ...
Run Code Online (Sandbox Code Playgroud)

网络方法

@WebMethod
public String sampleMethod(@WebParam(name = "params") SampleRequest params) {
Run Code Online (Sandbox Code Playgroud)

也许这会有所帮助

  • 如果我们这样做,“SampleRequestType”本身就会显示可选。 (3认同)