开始 WebService 入门示例

This commit is contained in:
YunaiV
2020-06-17 07:08:46 +08:00
parent e51ad0ab4f
commit 87639c6106
12 changed files with 218 additions and 172 deletions

View File

@@ -0,0 +1,95 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lab-65-cxf-ws-demo</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-65-cxf-ws-demo-application</artifactId>
<properties>
<!-- 依赖相关配置 -->
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<!-- 插件相关配置 -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 实现 CXF 对 Web Services 的自动配置 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>src/main/resources/wsdl/user.wsdl</wsdl>
<wsdlLocation>classpath:wsdl/user.wsdl</wsdlLocation>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- &lt;!&ndash; maven-jaxb2-plugin 插件,用于实现将 WSDL 生成目标类 &ndash;&gt;-->
<!-- <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>-->
<!-- &lt;!&ndash; WSDL 源文件地址 &ndash;&gt;-->
<!-- <schemaLanguage>WSDL</schemaLanguage>-->
<!-- <schemas>-->
<!-- <schema>-->
<!-- <url>http://127.0.0.1:8080/ws/users.wsdl</url>-->
<!-- </schema>-->
<!-- </schemas>-->
<!-- &lt;!&ndash; 生成代码目标包 &ndash;&gt;-->
<!-- <generatePackage>cn.iocoder.springboot.lab65.demo.wsdl</generatePackage>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>
</project>

View File

@@ -0,0 +1,19 @@
package cn.iocoder.springboot.lab65.demo.config;
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebServicesConfig {
@Bean
public UserService userService() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:8080/ws/user");
return (UserService) jaxWsProxyFactoryBean.create();
}
}

View File

@@ -0,0 +1,42 @@
package cn.iocoder.springboot.lab65.demo.controller;
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserGetRequest;
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserGetResponse;
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private UserService userService;
@GetMapping("/get")
public String get(@RequestParam("id") Integer id) {
UserGetRequest request = new UserGetRequest();
request.setId(id);
// 执行 Web Services 请求
UserGetResponse response = userService.get(request);
// 响应
return response.getName();
}
// @GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
// public Integer create(@RequestParam("name") String name,
// @RequestParam("gender") Integer gender) {
// // 请求
// UserCreateRequest request = new UserCreateRequest();
// request.setName(name);
// request.setGender(gender);
// // 执行 Web Services 请求
// UserCreateResponse response = userClient.createUser(name, gender);
// // 响应
// return response.getId();
// }
}

View File

@@ -0,0 +1,59 @@
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="userService" targetNamespace="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo" elementFormDefault="unqualified" targetNamespace="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo" version="1.0">
<xs:element name="get" type="tns:get"/>
<xs:element name="getResponse" type="tns:getResponse"/>
<xs:complexType name="get">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:userGetRequest"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="userGetRequest">
<xs:sequence>
<xs:element minOccurs="0" name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:userGetResponse"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="userGetResponse">
<xs:sequence>
<xs:element minOccurs="0" name="gender" type="xs:int"/>
<xs:element minOccurs="0" name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getResponse">
<wsdl:part element="tns:getResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="get">
<wsdl:part element="tns:get" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="UserService">
<wsdl:operation name="get">
<wsdl:input message="tns:get" name="get"> </wsdl:input>
<wsdl:output message="tns:getResponse" name="getResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="userServiceSoapBinding" type="tns:UserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="get">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="get">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="userService">
<wsdl:port binding="tns:userServiceSoapBinding" name="UserServiceImplPort">
<soap:address location="http://127.0.0.1:8080/ws/user"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

View File

@@ -1,77 +0,0 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lab-65-spring-ws-demo</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-65-spring-ws-demo-application</artifactId>
<properties>
<!-- 依赖相关配置 -->
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<!-- 插件相关配置 -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 实现对 Spring WebService 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- Java WSDL 实现库 -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- maven-jaxb2-plugin 插件,用于实现将 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>
<!-- WSDL 源文件地址 -->
<schemaLanguage>WSDL</schemaLanguage>
<schemas>
<schema>
<url>http://127.0.0.1:8080/ws/users.wsdl</url>
</schema>
</schemas>
<!-- 生成代码目标包 -->
<generatePackage>cn.iocoder.springboot.lab65.demo.wsdl</generatePackage>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,28 +0,0 @@
package cn.iocoder.springboot.lab65.demo.client;
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateRequest;
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateResponse;
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetRequest;
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class UserClient extends WebServiceGatewaySupport {
public UserGetResponse getUser(Integer id) {
// 创建请求对象
UserGetRequest request = new UserGetRequest();
request.setId(id);
// 执行请求
return (UserGetResponse) getWebServiceTemplate().marshalSendAndReceive(request);
}
public UserCreateResponse createUser(String name, Integer gender) {
// 创建请求对象
UserCreateRequest request = new UserCreateRequest();
request.setName(name);
request.setGender(gender);
// 执行请求
return (UserCreateResponse) getWebServiceTemplate().marshalSendAndReceive(request);
}
}

View File

@@ -1,29 +0,0 @@
package cn.iocoder.springboot.lab65.demo.config;
import cn.iocoder.springboot.lab65.demo.client.UserClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class WebServicesConfig {
// 创建 Jaxb2Marshaller Bean实现 XML 与 Bean 的互相转换
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("cn.iocoder.springboot.lab65.demo.wsdl"); // 用户服务的 WSDL 文件
return marshaller;
}
// 创建 UserClient Bean
@Bean
public UserClient countryClient(Jaxb2Marshaller marshaller) {
UserClient client = new UserClient();
client.setDefaultUri("http://127.0.0.1:8080/ws"); // 用户服务的 Web Services 地址
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}

View File

@@ -1,36 +0,0 @@
package cn.iocoder.springboot.lab65.demo.controller;
import cn.iocoder.springboot.lab65.demo.client.UserClient;
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateResponse;
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private UserClient userClient;
@GetMapping("/get")
public String get(@RequestParam("id") Integer id) {
// 执行 Web Services 请求
UserGetResponse response = userClient.getUser(id);
// 响应
return response.getName();
}
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
public Integer create(@RequestParam("name") String name,
@RequestParam("gender") Integer gender) {
// 执行 Web Services 请求
UserCreateResponse response = userClient.createUser(name, gender);
// 响应
return response.getId();
}
}

View File

@@ -13,7 +13,7 @@
<packaging>pom</packaging>
<modules>
<module>lab-65-cxf-ws-demo-user-service</module>
<!-- <module>lab-65-spring-ws-demo-application</module>-->
<module>lab-65-cxf-ws-demo-application</module>
</modules>

View File

@@ -20,6 +20,7 @@ public class DemoController {
@GetMapping("/get")
public String get(@RequestParam("id") Integer id) {
// 请求
UserGetRequest request = new UserGetRequest();
request.setId(id);
// 执行 Web Services 请求
@@ -31,7 +32,7 @@ public class DemoController {
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
public Integer create(@RequestParam("name") String name,
@RequestParam("gender") Integer gender) {
// 创建
// 请求
UserCreateRequest request = new UserCreateRequest();
request.setName(name);
request.setGender(gender);