【问题标题】:CXF @XmlElement(required=true) not workingCXF @XmlElement(required=true) 不工作
【发布时间】:2015-01-06 15:33:34
【问题描述】:

我有如下界面:

    @WebMethod(action = "create")
VoidResponse create(    
                                @WebParam(name="description") @XmlElement(required=true) String description, 
                                @WebParam(name="userId") @XmlElement(required=true) String userId, 
                                @WebParam(name="accountImage") String accountImage...);

这是生成的 wsdl 复杂类型:

<xs:complexType name="create">
<xs:sequence>
<xs:element name="description" type="xs:string"/>
<xs:element name="userId" type="xs:string"/>
<xs:element minOccurs="0" name="accountImage" type="xs:string"/>
</xs:sequence>
</xs:complexType>

我们可以看到,description 和 userId 是必填字段。 这似乎是正确的,但是,例如,当我提出请求、省略描述字段或将其发送为空时,CXF 不会引发 soapFault。

我做错了什么?

【问题讨论】:

  • 您可以为@XmlElement 添加您的导入语句吗?也许这是错误的。

标签: java soap jaxb xsd cxf


【解决方案1】:

默认情况下 cxf 不执行架构验证。您需要通过将以下设置添加到您的 Web 服务界面来手动启用它

@WebService(name = "abc", targetNamespace = "xyz", wsdlLocation="/path to wsdl")
@EndpointProperties(value = {
    @EndpointProperty(key="schema-validation-enabled", value="true")
})
public interface MyFirstSOAPWebService{
    @WebMethod(action = "create")
    VoidResponse create(    
                            @WebParam(name="description") @XmlElement(required=true) String description, 
                            @WebParam(name="userId") @XmlElement(required=true) String userId, 
                            @WebParam(name="accountImage") String accountImage...);
}

请注意:wsdlLocation 设置是强制性的,以及 schema-validation-enabled 因为 cxf 需要知道 xsd 来验证传入的请求。 wsdl 位置将提供所需的 xsds

【讨论】:

    【解决方案2】:

    您可以尝试在您的 jaxws:endpoint 定义上设置模式验证吗?如果您使用的是 Spring,则如下所示

    <jaxws:endpoint ...> <jaxws:properties> <entry key="schema-validation-enabled" value="true" /> </jaxws:properties> ... </jaxws:endpoint>

    甚至用@org.apache.cxf.annotations.SchemaValidation注释你的服务

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 2012-09-25
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多