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

        
            org.springframework.boot
            spring-boot-starter-web-services
        

    org.jvnet.jaxb2.maven2
    maven-jaxb2-plugin
    0.13.2
    
        
            
                generate
            
        
    
    
        com.example.howtodoinjava.schemas.school
        ${project.basedir}/src/main/java
        ${project.basedir}/src/main/resources/wsdl
        
            *.wsdl
        
    

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  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 [Sajal5Pune] for request [Sajal]
Got Response As below ========= :
Name : Lokesh
Standard : 6
Address : Delhi