建议大家在开始webservice相关学习时,先了解一下xml、xsd、soap、wsdl相关的知识点,我自身对这方面也不是很了解,有这方面大神的欢迎在评论区留言交流!!
   
    
    
    【继上文完成soap webservice的demo后,本篇文章完成soap webclient的说明】:
   
    
    
    1. SpringBoot Soap Client技术栈
   
- JDK 1.8
- SpringBoot
- maven-jaxb2-plugin
    
    
    2. 具体操作
   
    
    
    1. 在pom.xml中添加dependency和plugin
   
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</dependency>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.13.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generatePackage>com.qs.webclient.xml.student</generatePackage>
                    <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
                    <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                    <schemaIncludes>
                        <include>*.wsdl</include>
                    </schemaIncludes>
                </configuration>
            </plugin>
    
    
    2. resource文件下访问webService的wsdl文件
   
    (见第九步 项目目录截图说明)
    
    jaxb2插件与wsdl文件都弄好之后,执行该jaxb2插件,生成类
   
    
    
    3. 使用WebServiceTemplate创建SOAP Client
   
创建一个名为SOAPConnector.java的类,它将作为所有请求WebService的Client
package com.qs.webclient.soap;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
/**
 * 对Web Service的所有请求的通用Web Client
 *
 * @author QuS
 * @date 2022/3/17 14:54
 */
public class SOAPConnector extends WebServiceGatewaySupport {
    public Object callWebService(String url, Object request) {
        return getWebServiceTemplate().marshalSendAndReceive(url, request);
    }
}
- getWebServiceTemplate()将获得WebServiceTemplate;
- 使用WebServiceTemplate去调用SOAP service;
- 该类也需要设置Marshaller 和Unmarshaller,我们可以通过配置类的方式将其注入。
    
    
    4. Spring bean的配置类
   
配置一些SOAPConnector需要的bean,从而让它可以正常工作。
package com.qs.webclient.config;
import com.qs.webclient.soap.SOAPConnector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
/**
 * @author QuS
 * @date 2022/3/17 14:57
 */
@Configuration
public class SoapClientConfig {
    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.qs.webclient.xml.student");
        return marshaller;
    }
    @Bean
    public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
        SOAPConnector client = new SOAPConnector();
        client.setDefaultUri("http://localhost:9081/service/student-details");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}
- WebServiceGatewaySupport 需要Marshaller和Unmarshaller,他们都是Jaxb2Marshaller 的实例;
- 使用”com.qs.webclient.xml.student”作为JAXB基础包去创建JAXB context;
    
    
    5. 使用CommandLineRunner简单测试
   
package com.qs.webclient;
import com.qs.webclient.soap.SOAPConnector;
import com.qs.webclient.xml.student.StudentDetailsRequest;
import com.qs.webclient.xml.student.StudentDetailsResponse;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class WebclientApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebclientApplication.class, args);
    }
    @Bean
    CommandLineRunner lookup(SOAPConnector soapConnector) {
        return args -> {
            String name = "小明";//Default Name
            if (args.length > 0) {
                name = args[0];
            }
            StudentDetailsRequest request = new StudentDetailsRequest();
            request.setName(name);
            StudentDetailsResponse response = (StudentDetailsResponse) soapConnector.callWebService("http://localhost:9081/service/student-details", request);
            System.out.println("Got Response As below ========= : ");
            System.out.println("Name : " + response.getStudent().getName());
            System.out.println("Standard : " + response.getStudent().getStandard());
            System.out.println("Address : " + response.getStudent().getAddress());
        };
    }
}
    
    
    6. 在application.properties 添加如下配置
   
logging.level.org.springframework.ws=TRACE
这样,在控制台上将打印出send和response的日志消息,以便于检测。
    
    
    7. 启动springboot直接调用
   
    
    
    8. 输出
   
2022-03-17 17:19:07.558 DEBUG 1388 --- [           main] o.s.ws.client.core.WebServiceTemplate    : Opening [org.springframework.ws.transport.http.HttpUrlConnection@765df79d] to [http://localhost:9081/service/student-details]
2022-03-17 17:19:07.596 TRACE 1388 --- [           main] o.s.ws.client.MessageTracing.sent        : Sent request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsRequest xmlns:ns2="http://www.howtodoinjava.com/xml/school"><ns2:name>小明</ns2:name></ns2:StudentDetailsRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2022-03-17 17:19:07.615 TRACE 1388 --- [           main] o.s.ws.client.MessageTracing.received    : Received response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsResponse xmlns:ns2="http://www.howtodoinjava.com/xml/school"><ns2:Student><ns2:name>小明</ns2:name><ns2:standard>123</ns2:standard><ns2:address>北京朝阳区</ns2:address></ns2:Student></ns2:StudentDetailsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:StudentDetailsRequest xmlns:ns2="http://www.howtodoinjava.com/xml/school"><ns2:name>小明</ns2:name></ns2:StudentDetailsRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
Got Response As below ========= : 
Name : 小明
Standard : 123
Address : 北京朝阳区
    
    
    9. 项目目录截图说明
   
     
   
 
