webServiceTemplate.marshalSendAndReceive;Spring Boot SOAP Client – WebServiceTemplate 案例

1. SpringBoot Soap Client技术栈

  1. JDK 1.8
  2. SpringBoot
  3. 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.example.howtodoinjava.schemas.school</generatePackage>
        <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
        <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
        <schemaIncludes>
            <include>*.wsdl</include>
        </schemaIncludes>
    </configuration>
</plugin>

2. 使用WebServiceTemplate创建SOAP Client

创建一个名为SOAPConnector.java的类,它将作为所有请求WebService的Client

package com.example.howtodoinjava.springbootsoapclient;
 
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
 
public class SOAPConnector extends WebServiceGatewaySupport {
 
    public Object callWebService(String url, Object request){
        return getWebServiceTemplate().marshalSendAndReceive(url, request);
    }
}
  1. getWebServiceTemplate()将获得WebServiceTemplate;
  2. 使用WebServiceTemplate去调用SOAP service;
  3. 该类也需要设置Marshaller 和Unmarshaller,我们可以通过配置类的方式将其注入。

3. Spring bean的配置类

配置一些SOAPConnector需要的bean,从而让它可以正常工作。

package com.example.howtodoinjava.springbootsoapclient;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
 
@Configuration
public class Config {
    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        // this is the package name specified in the <generatePackage> specified in
        // pom.xml
        marshaller.setContextPath("com.example.howtodoinjava.schemas.school");
        return marshaller;
    }
 
    @Bean
    public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
        SOAPConnector client = new SOAPConnector();
        client.setDefaultUri("http://localhost:8080/service/student-details");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}
  1. WebServiceGatewaySupport 需要Marshaller和Unmarshaller,他们都是Jaxb2Marshaller 的实例;
  2. 使用"com.example.howtodoinjava.schemas.school"作为JAXB基础包去创建JAXB context;

4. 创建一个service包,在包中创建SoapClientService.java

@Service
public class SoapClientService {

    @Autowired
    SoapConnector soapConnector;

    public void soapClientSend() {
        String name = "Sajal";
        StudentDetailsRequest request = new StudentDetailsRequest();
        request.setName(name);
        StudentDetailsResponse response =(StudentDetailsResponse) soapConnector.callWebService("http://localhost:8080/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());
    }
}

5. 在application.properties 添加如下配置

logging.level.org.springframework.ws=TRACE

这样,在控制台上将打印出send和response的日志消息,以便于检测。

6. 创建测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class SoapClientServiceTest {
    @Autowired
    SoapClientService soapClientService;

    @Test
    public void test() {
        soapClientService.soapClientSend();
    }
}

7. 输出

2017-10-09 23:20:45.548 TRACE 9204 --- [           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="https://www.howtodoinjava.com/xml/school"><ns2:Student><ns2:name>Sajal</ns2:name><ns2:standard>5</ns2:standard><ns2:address>Pune</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="https://www.howtodoinjava.com/xml/school"><ns2:name>Sajal</ns2:name></ns2:StudentDetailsRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>]
Got Response As below ========= :
Name : Lokesh
Standard : 6
Address : Delhi

 

posted @ 2022-03-16 15:02  南北12345678  阅读(582)  评论(0编辑  收藏  举报