【问题标题】:@XmlElement(required=true) for @WebParam does not work@WebParam 的 @XmlElement(required=true) 不起作用
【发布时间】:2011-12-13 17:53:47
【问题描述】:

我正在使用 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) {
...
}}

为第一个 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>

为第二种网络方法生成的架构是:

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

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

【问题讨论】:

  • 这两种情况的任何代码示例?您面临的实际问题(例如异常、更好的行为描述等)?
  • 请添加错误的stacktrace
  • 当我添加@XmlElement 得到错误The annotation @XmlElement is disallowed for this location 例如public boolean validateRegistration(@XmlElement(required = true) @WebParam String devicedId。 Java 1.6.45

标签: java annotations jax-ws required


【解决方案1】:

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

例如

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

也许这会有所帮助

【讨论】:

  • 如果我们这样做,'SampleRequestType' 本身显示为可选。
【解决方案2】:

您尝试更改@WebParam 和@XmlElement 中的顺序?实际上我有一个 WS whit 这个:

public Persona consultarPersona(@WebParam(name = "cedula") @XmlElement(required=true, nillable=false, name="cedula", namespace="cedulaParam")  String cedula) 

而生成的描述是:

 <xsd:schema>
<xsd:import namespace="cedulaParam" schemaLocation="http://eniacstein-pc:8080/WSDL_Sample/GestorPersonas?xsd=2"/>
</xsd:schema>

和架构定义:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="cedulaParam">
<xs:element name="cedula" type="xs:string"/>
</xs:schema>

就是这样!!

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 获取注解@XmlElement is disallowed for this location。如何解决这个问题。使用 RSA 进行开发。如果我需要更新 cxf 版本,请帮助与版本的 maven 链接
    【解决方案4】:

    如果您收到以下错误消息:“此位置不允许使用注释 @XmlElement”,则可能是您使用了错误的导入语句。

    改成:

    import javax.xml.bind.annotation.XmlElement;
    

    由于 Eclipse 建议将另一个包作为第一个选项,这是一个非常常见的错误。

    【讨论】:

    • 对于那个我有@XmlElement not applicable to parameter
    【解决方案5】:

    也许我找到了解决方案。如果有人遇到同样的问题,只需按照所有人之前回答的顺序将这些注释完全按此顺序放置在您的 Web 服务合同(接口)中。

    @WebParam(name = "yourParam") @XmlElement(required = true)  YourParam param
    

    【讨论】:

    • 订单似乎无关紧要(针对 CFX 3.4.2 测试)。 @XmlElement(required = true) 确实可以完成这项工作,可能取决于 CXF 的数据绑定(默认为 JAXB 2.x)。谈论完成的工作:您需要打开模式验证(默认关闭)以让 CFX 检查正确数量的参数。否则你得到的只是一个正确的 WSDL 类型定义。
    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多