webServiceTemplate.marshalSendAndReceive;Spring Boot SOAP Client – WebServiceTemplate 案例
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.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); } }
- getWebServiceTemplate()将获得WebServiceTemplate;
- 使用WebServiceTemplate去调用SOAP service;
- 该类也需要设置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; } }
- WebServiceGatewaySupport 需要Marshaller和Unmarshaller,他们都是Jaxb2Marshaller 的实例;
- 使用"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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2020-03-16 gitblit 配置图文详解