springbootsoap:如何将@webservice初始化到springboot应用程序中?

tyg4sfes  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(423)

我正在将一个web服务迁移到SpringBoot中。从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 启动申请表百东网站。我尝试实现以下代码:

@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引导上下文中?
我需要手动编写整个合同类吗?
有人能给我提供同样的指导吗。谢谢

falq053o

falq053o1#

我有一个类似的项目,它的工作。
尝试制作界面而不是

@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,并在target中生成一些dto类。这些dto类可以复制到项目中,并像实际的dto一样使用它们。这是将xsd转换为dto以供springboot使用的简单方法。
为此,必须在pom.xml中添加以下依赖项

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

相关问题