CXF @XmlElement(required=true) 不工作

CXF @XmlElement(required=true) not working

我有以下界面:

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

正如我们所见,描述和用户 ID 是必填字段。 这似乎是正确的,但是,当我发出请求时,省略描述字段或将其发送为空,例如,CXF 不会抛出 soapFault。

我做错了什么?

您可以尝试在您的 jaxws:endpoint 定义上设置架构验证吗?如果您使用 Spring

,则如下所示

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

甚至用 @org.apache.cxf.annotations.SchemaValidation

注释您的服务

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

@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...);
}

Please note: wsdlLocation setting is mandatory, along with schema-validation-enabled because cxf needs to know the xsds to validate the incoming requests. wsdl location will provide the required xsds