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

标签 java annotations jax-ws required

我正在使用 JAX-WS 构建 Web 服务。我有一个奇怪的问题,即 @WebParam 的注释 @XmlElement(required=true) 在某些 @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) {
...
}}

第一个网络方法生成的模式是:

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

第二个网络方法生成的模式是:

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

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

最佳答案

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

关于java - @WebParam 的@XmlElement(required=true) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7869494/

相关文章:

java - 为什么说: "Checked exceptions are not forwarded in calling chain (propagated)"?

java - 下载后 jar 文件损坏

java - 使用 XJC 生成 Java 源代码时使用 JAXWS enableWrapperStyle

java - 将注解应用于继承自@MappedSuperclass 的字段

java - 使用 JAX-WS for SOAP 的 WSDL 之外的信息?

java - 在每个请求之前获取新的 JAX-WS WebService 端口

java - 过滤后的 JTable 正确行号

java - 在 JTextField 中使用 evt.consume() 将小写转换为大写

Java - 使用 javadoc 和方法注释的约定?