简体   繁体   English

Spring 启动 Soap:如何将@WebService 启动到 Spring 启动应用程序中?

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

I am migrating a web service into spring boot.我正在将 web 服务迁移到 spring 引导中。 From wsdl i am able to generate following interface从 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;

I am not sure how to initiate it in Spring Boot application form baeldung website.我不知道如何在 Spring Boot application form baeldung website 中启动它。 I tried implementing following code:我尝试实现以下代码:

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

But I am not sure how to initiate above interface or it's implementation into spring boot context?但我不确定如何启动上述接口或它在 spring 引导上下文中的实现?

Do i need to write whole contract classes manually?我需要手动编写整个合同类吗?

Can anyone guide me about same.任何人都可以指导我。 Thanks谢谢

I have a similar project and it works.我有一个类似的项目,它有效。

Try making your interface instead of尝试制作你的界面而不是

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

like this像这样

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

Also in your configurations同样在您的配置中

wsdl11Definition.setTargetNamespace("Terp-v2");

Also in your pom.xml configure the build like following同样在您的 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>

After you build your project it will compile your xsd and will produce in target some DTO classes.构建项目后,它将编译您的 xsd 并在目标中生成一些 DTO 类。 Those DTO classes you can copy then in your project and use them like actual DTOs.您可以将那些 DTO 类复制到您的项目中,并像实际的 DTO 一样使用它们。 That's the easies way to convert your XSD to DTO to be used by spring boot.这是将 XSD 转换为 DTO 以供 spring 引导使用的简单方法。

To do this you must add the following dependency in your pom.xml为此,您必须在 pom.xml 中添加以下依赖项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM