Spring WS Consume Soap Service From WSDL

Spring WS Consume Soap Service From WSDL

BY MEMORYNOTFOUND · DECEMBER 31, 2015

This tutorial Spring WS Consume Soap Service from WSDL shows you how to Consume a Soap Web Service from a WSDL (Web Service Description Language) file. Spring WS provides a simple client-side Web Service API. It uses the WebServiceTemplate obtained from the WebServiceGatewaySupport for sending and receiving Soap Messages. In our previous example we saw how to produce soap web service using spring ws, in this example we will implement a client for this service.

Maven Configuration

For generating the Java Classes from WSDL we use the maven-jaxb2-plugin. We configure the WSDL schema location of the service which we want to implement and the generatedPackage specifying in which package structure the Java Classes will be created.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.spring.ws</groupId>
    <artifactId>consume-soap-web-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>SPRING-WS - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-ws</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.13.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>com.memorynotfound.beer</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://localhost:8080/ws/beers.wsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Generating Java Classes From WSDL

After running the following command, the Java Classes will be generated in the target/generated-sources/xjc/<package-name> folder.

mvn package

Spring WS Consume Soap Service Client

We can extend from the WebServiceGatewaySupport from which we obtain the WebServiceTemplatewhich we can use for marshalling, sending and receiving the requests and responses.

package com.memorynotfound.client;

import com.memorynotfound.beer.GetBeerRequest;
import com.memorynotfound.beer.GetBeerResponse;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

public class BeerClient extends WebServiceGatewaySupport {

    public GetBeerResponse getBeer(int id) {
        GetBeerRequest request = new GetBeerRequest();
        request.setId(id);
        return (GetBeerResponse) getWebServiceTemplate().marshalSendAndReceive(request);
    }
}

Spring Java Configuration

The @Configuration annotations tells spring that this Java Class is used as configuration file.

We create a Jaxb2Marshaller to automatically create Java Objects to and from xml request/response. We need to register a contextPath, this is the location of our JAX-B annotated resources. In this example they are created in the com.memorynotfound.beer package specified in the maven configuration.

Finally we configure the BeerClient which extends the WebServiceGatewaySupport from where we must set the defaultUrimarshaller and unmarshaller.

package com.memorynotfound.client;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class SoapClientConfig {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.memorynotfound.beer");
        return marshaller;
    }

    @Bean
    public BeerClient weatherClient(Jaxb2Marshaller marshaller) {
        BeerClient client = new BeerClient();
        client.setDefaultUri("http://localhost:8080/ws/beer");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

Testing Soap Client

We bootstrap the application using the AnnotationConfigApplicationContext which we pass in our SoapClientConfig Spring Java Configuration file. Then we obtain our BeerClient which we can use to perform our Soap Service.

package com.memorynotfound.client;

import com.memorynotfound.beer.GetBeerResponse;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class RunClient {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SoapClientConfig.class);
        BeerClient client = context.getBean(BeerClient.class);
        GetBeerResponse response = client.getBeer(1);
        System.out.println("Beer response: " + response.getBeer().getName());
    }

}

转载于:https://my.oschina.net/itwangxinli/blog/850548

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值