Spring 启动 ws:端点没有适配器

Spring boot ws: No adapter for endpoint

我正在尝试使用 spring boot ws 来创建 SOAP 服务,但我遇到了异常 "No adapter for endpoint..."。

这是我的 xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://nisws.it.fastweb"
           targetNamespace="http://nisws.it.fastweb" elementFormDefault="qualified">

    <xs:element name="doEventRequest" type="tns:doEventRequest"/>

    <xs:element name="doEventResponse" type="tns:doEventResponse"/>
    <xs:complexType name="doEventRequest">
        <xs:sequence>
            <xs:element name="header" type="tns:header"/>
            <xs:element name="trackingbody" type="tns:trackingbody"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="header">
        <xs:sequence>
            <xs:element name="IdMessaggio" type="xs:string"/>
            <xs:element name="NomeBE" type="xs:string"/>
            <xs:element name="DataInserimentoBE" type="xs:string"/>
            <xs:element name="Release" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="trackingbody">
        <xs:all>
            <xs:element name="Sorgente" type="xs:string"/>
            <xs:element name="Parametri" type="tns:Parametri" minOccurs="0"/>
            <xs:element name="Componenti" type="tns:Componenti" minOccurs="0"/>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="Parametri">
        <xs:sequence>
            <xs:element name="Parametro" type="tns:TagParametro" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="TagParametro">
        <xs:sequence>
            <xs:element name="Descrizione" type="xs:string"/>
            <xs:element name="Valore" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Componenti">
        <xs:sequence>
            <xs:element name="Componente" type="tns:TagComponente" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="TagComponente">
        <xs:sequence>
            <xs:element name="ID" type="xs:string" minOccurs="0"/>
            <xs:element name="Descrizione" type="xs:string" minOccurs="0"/>
            <xs:element name="Tipo" type="xs:string"/>
            <xs:element name="Parametri" type="tns:Parametri"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="doEventResponse">
        <xs:sequence>
            <xs:element name="header" type="tns:header"/>
            <xs:element name="trackingbody" type="tns:trackingbody"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

这是 servlet

的 class
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/nis/*");
    }

    @Bean(name = "nisDoEvent")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema doEventSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("nisDoEvent");
        wsdl11Definition.setLocationUri("/nis/ws");
        wsdl11Definition.setTargetNamespace("http://nisws.it.fastweb");
        wsdl11Definition.setSchema(doEventSchema);
//      wsdl11Definition.setRequestSuffix("doEvent");
        return wsdl11Definition;
    }

    @Bean (name = "doEventSchema")
    public XsdSchema doEventSchema() {
        return new SimpleXsdSchema(new ClassPathResource("doEventSchema.xsd"));
    }

}

这是我的端点

@Endpoint
public class CompletaMigrBNGEndpoint {

    private static final String NAMESPACE_URI = "http://nisws.it.fastweb";

    private static final String EXAMPLE_NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";

    private CountryRepository countryRepository;

    @Autowired
    public CompletaMigrBNGEndpoint(CountryRepository countryRepository) {
        this.countryRepository = countryRepository;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "doEventRequest")
    @ResponsePayload
    public DoEventResponse doEvent(@RequestPayload DoEventRequest request){
        DoEventResponse response = new DoEventResponse();
        response.setHeader(request.getHeader());
        response.setTrackingbody(request.getTrackingbody());
        return response;
    }

    @PayloadRoot(namespace = EXAMPLE_NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();
        response.setCountry(countryRepository.findCountry(request.getName()));

        return response;
    }

如您所见,我也尝试遵循指南“https://spring.io/guides/gs/producing-web-service/

并且提供的示例工作正常。

关于我自己的服务,我尝试通过 soapui 调用它,但出现异常:

16:43:38.674 [http-nio-8080-exec-4] DEBUG o.s.w.s.server.SoapMessageDispatcher - Endpoint invocation resulted in exception - responding with Fault
java.lang.IllegalStateException: No adapter for endpoint [public org.hp.doEventSchema.DoEventResponse org.hp.resources.connection.soap.services.CompletaMigrBNGEndpoint.doEvent(org.hp.doEventSchema.DoEventRequest)]: Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?
    at org.springframework.ws.server.MessageDispatcher.getEndpointAdapter(MessageDispatcher.java:302) [spring-ws-core-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
    at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:235) [spring-ws-core-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
    at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:176) [spring-ws-core-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]

我认为我的错误可能出在 xsd 或端点映射中,但我无法修复它。

提前致谢

此致

我解决了使用 JAXBElement 封装由 jaxb 插件生成的 request/response 对象的问题。这就是为什么我注意到在那些 类 中没有 @XmlRootElement 注释。

谢谢大家

此致

只需像下面这样用 JAXBElement 包装请求和响应

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "doEventRequest")
    @ResponsePayload
    public JAXBElement< DoEventResponse > doEvent(@RequestPayload JAXBElement< DoEventRequest > request){}

我们可以像下面这样调用来获取对象

DoEventRequest d = request.getValue();