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

时间:2021-02-05 10:05:37

标签: java spring spring-boot soap

我正在将 Web 服务迁移到 Spring Boot。 从 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 应用程序表单 baeldung 网站中启动它。我尝试实现以下代码:

@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 boot 上下文中的实现?

我需要手动编写整个合约类吗?

任何人都可以指导我。谢谢

1 个答案:

答案 0 :(得分:1)

我有一个类似的项目,而且效果很好。

尝试制作您的界面而不是

@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 转换为 Spring Boot 使用的 DTO 的简单方法。

为此,您必须在 pom.xml 中添加以下依赖项

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