簡體   English   中英

Spring 啟動 Soap:如何將@WebService 啟動到 Spring 啟動應用程序中?

[英]Spring Boot Soap :How to initiate @WebService into Spring Boot Application?

我正在將 web 服務遷移到 spring 引導中。 從 wsdl 我能夠生成以下接口

@WebService(targetNamespace = "http://tsb.hcl.com/terp/ws/v2", name = "Terp-v2")
@XmlSeeAlso({com.hcl.psi.tsb.schema.common.ObjectFactory.class, com.hcl.psi.tsb.schema.offering.ObjectFactory.class, com.hcl.psi.kil.system.message.ObjectFactory.class, com.hcl.tsb.terp.ws.schema.ObjectFactory.class, com.hcl.psi.tsb.schema.task.ObjectFactory.class, com.hcl.psi.tsb.schema.servicecontract.ObjectFactory.class, com.hcl.psi.tsb.schema.project.ObjectFactory.class, com.hcl.tsb.tsb.ObjectFactory.class, com.hcl.psi.tsb.schema.customer.ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface TerpV2 {

    @WebMethod(operationName = "GetServiceContracts")
    @WebResult(name = "GetServiceContractsResponseType", targetNamespace = "http://tsb.hcl.com/terp/ws/schema", partName = "payload")
    public com.hcl.tsb.terp.ws.schema.GetServiceContractsResponseType getServiceContracts(
        @WebParam(partName = "payload", name = "GetServiceContractsType", targetNamespace = "http://tsb.hcl.com/terp/ws/schema")
        com.hcl.tsb.terp.ws.schema.GetServiceContractsType payload
    ) throws TerpServiceFault;

我不知道如何在 Spring Boot application form baeldung website 中啟動它。 我嘗試實現以下代碼:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

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


    @Bean(name = "TerpService")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("Terp-v2");
        wsdl11Definition.setLocationUri("/tsb/tone/ws/v2/TerpService/");
        wsdl11Definition.setTargetNamespace("http://www.baeldung.com/springsoap/gen");
        wsdl11Definition.setSchema(countriesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
    }
}

但我不確定如何啟動上述接口或它在 spring 引導上下文中的實現?

我需要手動編寫整個合同類嗎?

任何人都可以指導我。 謝謝

我有一個類似的項目,它有效。

嘗試制作你的界面而不是

@WebService(targetNamespace = "http://tsb.hcl.com/terp/ws/v2", name = "Terp-v2")
@XmlSeeAlso({com.hcl.psi.tsb.schema.common.ObjectFactory.class, com.hcl.psi.tsb.schema.offering.ObjectFactory.class, com.hcl.psi.kil.system.message.ObjectFactory.class, com.hcl.tsb.terp.ws.schema.ObjectFactory.class, com.hcl.psi.tsb.schema.task.ObjectFactory.class, com.hcl.psi.tsb.schema.servicecontract.ObjectFactory.class, com.hcl.psi.tsb.schema.project.ObjectFactory.class, com.hcl.tsb.tsb.ObjectFactory.class, com.hcl.psi.tsb.schema.customer.ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface TerpV2 {

    @WebMethod(operationName = "GetServiceContracts")
    @WebResult(name = "GetServiceContractsResponseType", targetNamespace = "http://tsb.hcl.com/terp/ws/schema", partName = "payload")
    public com.hcl.tsb.terp.ws.schema.GetServiceContractsResponseType getServiceContracts(
        @WebParam(partName = "payload", name = "GetServiceContractsType", targetNamespace = "http://tsb.hcl.com/terp/ws/schema")
        com.hcl.tsb.terp.ws.schema.GetServiceContractsType payload
    ) throws TerpServiceFault;

像這樣

@Endpoint
public class TerpV2 {

    private static final String NAMESPACE_URI = "Terp-v2";
    
 @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetServiceContractsType")   
 @ResponsePayload
public YourCustomResponseObject getServiceContracts(@RequestPayload GetServiceContractsType getServiceContractsType) {
    //this could be constructed in a service class
    YourCustomResponseObject response = new YourCustomResponseObject();
    
    return response;
}

同樣在您的配置中

wsdl11Definition.setTargetNamespace("Terp-v2");

同樣在您的 pom.xml 中配置構建,如下所示

<build>
        <plugins>
            <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- Configuration excecutable=true i have added this to make the jar excecutable-->
            <configuration>
                <executable>true</executable>
            </configuration>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <!-- Configuration classifier=exec i have added this to force maven create one Fat
                        excecutable jar but also another jar for the dependencies of the payload modules-->
                        <!--  <classifier>exec</classifier>-->
                    </configuration>
                </execution>
            </executions>
        </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>
                        <source>src/main/resources/countries.xsd</source>
                    </sources>
                </configuration>
            </plugin>
        </plugins>
    </build>

構建項目后,它將編譯您的 xsd 並在目標中生成一些 DTO 類。 您可以將那些 DTO 類復制到您的項目中,並像實際的 DTO 一樣使用它們。 這是將 XSD 轉換為 DTO 以供 spring 引導使用的簡單方法。

為此,您必須在 pom.xml 中添加以下依賴項

        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM