WebService
WebService是一种跨编程语言和跨操作系统平台的远程调用技术
XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术
目录结构
将wsdl文档放入resources.wsdl中(或者在maven中配置wsdl url)
其中wdsl.message和wdsl.voice是相应生成的模板类
目录结构
pom.xml
通过maven插件jaxb2生成模板类
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consuming-web-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consuming-web-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- tag::dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- end::dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generatePackage>com.example.consumingwebservice.wsdl.voice</generatePackage>
<generateDirectory>${project.basedir}/src/main/java</generateDirectory>
<schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
<schemaIncludes>
<include>shortMessage.wsdl</include>
</schemaIncludes>
<!--<schemas>-->
<!--<schema>-->
<!--<url>http://localhost:8080/ws/sendMessage.wsdl</url>-->
<!--</schema>-->
<!--</schemas>-->
</configuration>
</plugin>
<!-- end::wsdl[] -->
</plugins>
</build>
</project>
configbean配置
@Configuration
public class WebServiceConfig {
@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.consumingwebservice.wsdl.voice");
return marshaller;
}
@Bean
public WebServiceClient soapConnector(Jaxb2Marshaller marshaller) {
WebServiceClient client = new WebServiceClient();
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
public class WebServiceClient extends WebServiceGatewaySupport {
public Object callWebService(String url, Object request, String callbackUrl){
return getWebServiceTemplate().marshalSendAndReceive(url, request, new SoapActionCallback(callbackUrl));
}
}
使用
@SpringBootTest
class SendMessageTests {
@Autowired
private WebServiceClient callClient;
@Test
void test() throws DatatypeConfigurationException {
//SendMessage是生成的模板类请求参数
SendMessage request = new SendMessage();
//设置参数 略
request.setContent("测试3");
//webservice地址
String url = "xxx";
//callback是回调地址,将请求结果返回;即soapAction地址
String callback = "xxx";
SendMessageResponse response = (SendMessageResponse) callClient.callWebService(url,request,callback);
System.out.println(response.getSendMessageResult().isSuccess());
System.out.println(response.getSendMessageResult().getResult());
System.out.println(response.getSendMessageResult().getMessage());
}
}
参考文档
//www.greatytc.com/p/49d7997ad3b7
https://howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/
https://spring.io/guides/gs/consuming-web-service/#scratch