具有原始数据类型的@Endpoint - 端点没有适配器

@Endpoint with primitive data types - No adapter for endpoint

我正在尝试使用 gradle (WSDL2Java) 从 WSDL 生成 CXF-Java-类,以便我可以在我的 SpringBoot 中使用它-申请.

应用程序提供了一个 SOAP 端点。我想要的响应仅包含一个简单的布尔值:

要求:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header/>
   <soap:Body>
      <ns:isActiveRequest xmlns:ns=\"http://problem.com\"><id>1234</id></ns:isActiveRequest>
   </soap:Body>
</soap:Envelope>

回复:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header/>
   <soap:Body>
      <ns:isActiveResponse xmlns:ns="http://problem.com">true</ns3:isActiveResponse>
   </soap:Body>
</soap:Envelope>

我无法在我的 WSDL 中定义此响应,因此它在我的 SpringBoot 应用程序中使用 @Endpoint-Annotation 生成 CXF 后工作。我总是收到错误消息:"No adapter for endpoint ... Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?"

通常我对具有复杂响应类型的 CXF 和 Endpoint 没有问题,因为它会生成一个 @XmlRootElement。但它不会在响应中使用原始类型来执行此操作,并且无法在 @Endpoint-Annotation 下识别我的方法。

[...]
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class SoapEndpoint implements Problem {
private static final String TARGET_NAMESPACE = "http://problem.com";

@PayloadRoot(namespace = TARGET_NAMESPACE, localPart = "isActiveRequest")
@ResponsePayload
@Secured(Roles.PERMISSION)
public boolean isActive(@RequestPayload IsActiveRequest request) {
    return true;
}

我用于生成的 WSDL 如下所示:

<wsdl:types>
    <xs:schema attributeFormDefault="unqualified"
               elementFormDefault="unqualified" targetNamespace="http://problem.com"
               xmlns:xs="http://www.w3.org/2001/XMLSchema">

        <xs:element name="isActiveRequest">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="id" maxOccurs="1" minOccurs="1"
                                nillable="false" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="isActiveResponse" type="xs:boolean"/>

    </xs:schema>
</wsdl:types>

<wsdl:message name="isActiveRequest">
    <wsdl:part name="parameters" element="tns:isActiveRequest"></wsdl:part>
</wsdl:message>
<wsdl:message name="isActiveResponse">
    <wsdl:part name="parameters" element="tns:isActiveResponse"></wsdl:part>
</wsdl:message>

<wsdl:portType name="problem">
    <wsdl:operation name="isActive">
        <wsdl:input message="tns:isActiveRequest" name="isActiveRequest"/>
        <wsdl:output message="tns:isActiveResponse" name="isActiveResponse"/>
        <wsdl:fault message="tns:myException" name="fault"/>
    </wsdl:operation>
</wsdl:portType>

<wsdl:binding name="problem_SOAPBinding" type="tns:problem">
    <soap:binding style="document"
                  transport="http://schemas.xmlsoap.org/soap/http"/>

    <wsdl:operation name="isActive">
        <soap:operation soapAction="isActive"/>
        <wsdl:input name="isActiveRequest">
            <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output name="isActiveResponse">
            <soap:body use="literal"/>
        </wsdl:output>
        <wsdl:fault name="fault">
            <soap:fault use="literal" name="fault"/>
        </wsdl:fault>
    </wsdl:operation>
</wsdl:binding>

在我的 build.gradle 中,我使用的是 CXF 3.2.1

task wsdl2java(type: JavaExec) {
    ext {
        outputDir = file(cxfOutputDir)
        wsdlFiles = new FileNameByRegexFinder().getFileNames("${wsdlDir}", /.*\.wsdl/)
    }

    wsdlFiles.each { String wsdlFile ->
        outputs.upToDateWhen { false }
        outputs.dir outputDir
        main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
        classpath = configurations.cxfTool
        args '-d', outputDir
        args '-wsdlLocation', "/wsdl/" + new File(wsdlFile).name
        args '-verbose'
        args '-validate'
        args wsdlFile
    }
}

compile group: 'org.apache.cxf', name: 'cxf-core', version: "3.2.1"
compile group: 'org.apache.cxf', name: 'cxf-rt-frontend-jaxws', version: "3.2.1"
compile "org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.1"
cxfTool "org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:3.2.1"
cxfTool "org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:3.2.1"
cxfTool "org.apache.cxf:cxf-tools-common:3.2.1"
cxfTool "org.apache.cxf:cxf-tools-wsdlto-core:3.2.1"

默认没有原始类型MethodArgumentResolver。 如参考资料所示,您可以使用 10 多个不同的解析器。 我最终得到了 JAXB:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "RemoveObject")
@ResponsePayload
public RemoveObjectResponse removeObject(@RequestPayload JAXBElement<Integer> objectID, @RequestPayload JAXBElement<Boolean> useTrashbin, @RequestPayload JAXBElement<Boolean> ignoreReferences) {
...
}

或者您可以为原始类型编写自己的扩展,但很可能不值得。

https://docs.spring.io/spring-ws/site/reference/html/server.html#d5e1086

如果您为 org.springframework.xml 记录器启用跟踪日志,那么您可以看到潜在参数解析器的日志条目 Testing if argument resolver ... supports <your parameter>

我已经解决了 JAXBElement<>

的问题
public JAXBElement<Boolean> isActive(@RequestPayload IsActiveRequest request) {
   ...
   ObjectFactory factory = new ObjectFactory();
   return factory.createIsActiveResponse(myBoolean);
}