Springboot starter web service consume a soap message

参考Spring官方文档:https://spring.io/guides/gs/consuming-web-service/

本文代码已上传到Github: https://github.com/michaelzhanghe/ws-learning-consumer.git

服务端文章请参考:Springboot starter web service produce a soap message

WSDL 文件countries.wsdl.xml在根目录下


pom.xml中添加依赖关系和生成客户端代码的maven plugin:

<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>org.wslearning</groupId>
	<artifactId>ws-learning-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>ws-learning-consumer</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<!-- WebService client requrie this dependency for handling WebService request -->  
			<groupId>org.springframework.ws</groupId>
			<artifactId>spring-ws-core</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- tag::wsdl[] -->
			<plugin>
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<!-- maven plug-in for generating the client source code -->
				<artifactId>maven-jaxb2-plugin</artifactId>
				<version>0.13.3</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<schemaLanguage>WSDL</schemaLanguage>
					<!-- Location for generating the WebService client class source code by WSDL URL -->
					<generatePackage>org.wslearning.ws_learning_consumer.countries</generatePackage>
					<schemas>
						<schema>
						<!-- WebService WSDL URL --> 
							<url>http://127.0.0.1:8080/ws/countries.wsdl</url>
						</schema>
					</schemas>
				</configuration>
			</plugin>
			<!-- end::wsdl[] -->
		</plugins>
	</build>
</project>

通过Maven的jaxb2插件配置WSDL后,生成客户端代码



Copy生成的client端代码到org.wslearning.ws_learning_consumer.countries

创建WS client (CountryClient), 最关键的地方到了,对照WSDL文件仔细看注释和代码。

package org.wslearning.ws_learning_consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.wslearning.ws_learning_consumer.countries.GetCountryRequest;
import org.wslearning.ws_learning_consumer.countries.GetCountryResponse;


public class CountryClient extends WebServiceGatewaySupport {

	private static final Logger log = LoggerFactory.getLogger(CountryClient.class);

	public GetCountryResponse getCountry(String ticker) {

		
		GetCountryRequest request = new GetCountryRequest(); 
		String name = "Spain";

		log.info("Requesting country name for " + name);
		
//		name refer to below
//		<xs:element name="getCountryRequest">
//		<xs:complexType>
//		<xs:sequence>
//		<xs:element name="name" type="xs:string"/>
//		</xs:sequence>
//		</xs:complexType>
//		</xs:element>
		request.setName(name);
		
//		http://127.0.0.1:8080/ws refer to "soap:address location" from wsdl file as below sample
//		<wsdl:service name="CountriesPortService">
//		<wsdl:port binding="tns:CountriesPortSoap11" name="CountriesPortSoap11">
//		<soap:address location="http://127.0.0.1:8080/ws"/>
		
//		http://www.wslearning.org/countries refer to targetNamespace="http://www.wslearning.org/countries" in WSDL file
		
//		getCountry refer to wsdl:operation name="getCountry" in WSDL as below
//		<wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
//		<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
//		<wsdl:operation name="getCountry">
//		<soap:operation soapAction=""/>
//		<wsdl:input name="getCountryRequest">
//		<soap:body use="literal"/>
//		</wsdl:input>
//		<wsdl:output name="getCountryResponse">
//		<soap:body use="literal"/>
//		</wsdl:output>
//		</wsdl:operation>
//		</wsdl:binding>
		
//		request refer to <wsdl:input name="getCountryRequest">
		
//		response refer to <wsdl:output name="getCountryResponse">
		
		GetCountryResponse response = (GetCountryResponse) getWebServiceTemplate()
				.marshalSendAndReceive("http://127.0.0.1:8080/ws",
						request,
						new SoapActionCallback("http://www.wslearning.org/countries/getCountry"));

		return response;
	}

}


创建配置类为了之后使用WS client(CountryClient):

package org.wslearning.ws_learning_consumer;

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

@Configuration
public class CountryConfiguration {

	@Bean
	public Jaxb2Marshaller marshaller() {
		Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
		// this ContextPath(i.e. package) must match the value of <generatePackage> specified in pom.xml
		
		marshaller.setContextPath("org.wslearning.ws_learning_consumer.countries");
		return marshaller;
	}

	@Bean
	public CountryClient countryClient(Jaxb2Marshaller marshaller) {
		CountryClient client = new CountryClient();
//		http://127.0.0.1:8080/ws is refer to "soap:address location" from wsdl file as below 
//		<wsdl:service name="CountriesPortService">
//		<wsdl:port binding="tns:CountriesPortSoap11" name="CountriesPortSoap11">
//		<soap:address location="http://127.0.0.1:8080/ws"/>
		client.setDefaultUri("http://127.0.0.1:8080/ws");
		client.setMarshaller(marshaller);
		client.setUnmarshaller(marshaller);
		return client;
	}

}

创建一个controller为了能够在web上面通过rest接口调用webservice

package org.wslearning.ws_learning_consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.wslearning.ws_learning_consumer.countries.Country;
import org.wslearning.ws_learning_consumer.countries.GetCountryResponse;

@RestController
public class CountryController {

	@Autowired
	CountryClient countryClient;
	
	@GetMapping(name="/country/{countryName}")
	public Country getCountry(@PathVariable(required=false) String countryName) {
		 

		if (countryName == null || countryName.equals("")) {
			countryName = "Spain";
		}
		GetCountryResponse response = countryClient.getCountry(countryName);
		return response.getCountry();
	}
}


创建Springboot的入口类:

package org.wslearning.ws_learning_consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Michaelehome
 * @date 28-Mar-2018
 */
@SpringBootApplication
public class Application 
{
    public static void main( String[] args )
    {
        System.out.println( "Consume a soap web service message start....." );
        SpringApplication.run(Application.class, args);
    }
}


修改端口为8081,因为webservice 服务端口是8080,这个要区分开。



启动服务端和和客户端,然后输入网址查看: http://127.0.0.1:8081/country


阅读更多
个人分类: WebService
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭