Skip to content

Commit e51ad0a

Browse files
author
YunaiV
committed
开始 WebService 入门示例
1 parent 0ab7e92 commit e51ad0a

File tree

19 files changed

+421
-3
lines changed

19 files changed

+421
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-65-spring-ws-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-65-cxf-ws-demo-user-service</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-parent</artifactId>
27+
<version>${spring.boot.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<!-- 实现 CXF 对 Web Services 的自动配置 -->
36+
<dependency>
37+
<groupId>org.apache.cxf</groupId>
38+
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
39+
<version>3.3.6</version>
40+
</dependency>
41+
</dependencies>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.iocoder.springboot.lab65.userservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class UserServiceApplication {
8+
9+
public static void main(String[] args) {
10+
// 启动 Spring Boot 应用
11+
SpringApplication.run(UserServiceApplication.class, args);
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.iocoder.springboot.lab65.userservice.config;
2+
3+
import cn.iocoder.springboot.lab65.userservice.service.UserService;
4+
import org.apache.cxf.Bus;
5+
import org.apache.cxf.bus.spring.SpringBus;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import javax.xml.ws.Endpoint;
10+
11+
@Configuration
12+
public class WebServicesConfig {
13+
14+
public static final String NAMESPACE_URI = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
15+
16+
@Bean(name = Bus.DEFAULT_BUS_ID)
17+
public SpringBus springBus() {
18+
return new SpringBus();
19+
}
20+
21+
@Bean
22+
public Endpoint endpoint(UserService userService) {
23+
Endpoint endpoint = Endpoint.create(userService);
24+
endpoint.publish("/user");//发布地址
25+
return endpoint;
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.iocoder.springboot.lab65.userservice.request;
2+
3+
/**
4+
* 获得用户信息 Request
5+
*/
6+
public class UserGetRequest {
7+
8+
/**
9+
* 用户编号
10+
*/
11+
private Integer id;
12+
13+
public Integer getId() {
14+
return id;
15+
}
16+
17+
public UserGetRequest setId(Integer id) {
18+
this.id = id;
19+
return this;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.iocoder.springboot.lab65.userservice.response;
2+
3+
/**
4+
* 获得用户信息 Response
5+
*/
6+
public class UserGetResponse {
7+
8+
/**
9+
* 用户编号
10+
*/
11+
private Integer id;
12+
/**
13+
* 昵称
14+
*/
15+
private String name;
16+
/**
17+
* 性别别
18+
*/
19+
private Integer gender;
20+
21+
public Integer getId() {
22+
return id;
23+
}
24+
25+
public UserGetResponse setId(Integer id) {
26+
this.id = id;
27+
return this;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public UserGetResponse setName(String name) {
35+
this.name = name;
36+
return this;
37+
}
38+
39+
public Integer getGender() {
40+
return gender;
41+
}
42+
43+
public UserGetResponse setGender(Integer gender) {
44+
this.gender = gender;
45+
return this;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.lab65.userservice.service;
2+
3+
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
4+
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
5+
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
6+
7+
import javax.jws.WebService;
8+
9+
@WebService(targetNamespace = WebServicesConfig.NAMESPACE_URI
10+
// ,
11+
// name = "userPortName"
12+
)
13+
public interface UserService {
14+
15+
UserGetResponse get(UserGetRequest request);
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.iocoder.springboot.lab65.userservice.service;
2+
3+
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
4+
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
5+
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
6+
import org.springframework.stereotype.Service;
7+
8+
import javax.jws.WebService;
9+
10+
@WebService(
11+
serviceName = "userService", // 服务名称
12+
targetNamespace = WebServicesConfig.NAMESPACE_URI, // WSDL 命名空间
13+
endpointInterface = "cn.iocoder.springboot.lab65.userservice.service.UserService" // Web Services 对应接口
14+
// name = "userPortType", // portType 名称,作为客户端生成代码时的接口名称 TODO
15+
// portName = "userPortName" // port 名称 TODO
16+
)
17+
@Service
18+
public class UserServiceImpl implements UserService {
19+
20+
@Override
21+
public UserGetResponse get(UserGetRequest request) {
22+
UserGetResponse response = new UserGetResponse();
23+
response.setId(request.getId());
24+
response.setName("没有昵称:" + request.getId());
25+
response.setGender(request.getId() % 2 + 1);
26+
return response;
27+
}
28+
29+
// @PayloadRoot(namespace = WebServicesConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
30+
// @ResponsePayload
31+
// public UserCreateResponse create(@RequestPayload UserCreateRequest request) {
32+
// UserCreateResponse response = new UserCreateResponse();
33+
// response.setId((int) (System.currentTimeMillis() / 1000));
34+
// return response;
35+
// }
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CXF 配置项,对应 CxfProperties 配置类
2+
cxf:
3+
path: /ws/ # CXF CXFServlet 的匹配路径
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-65-spring-ws-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-65-spring-ws-demo-application</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-parent</artifactId>
27+
<version>${spring.boot.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<!-- 实现对 Spring WebService 的自动化配置 -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web-services</artifactId>
39+
</dependency>
40+
41+
<!-- Java WSDL 实现库 -->
42+
<dependency>
43+
<groupId>wsdl4j</groupId>
44+
<artifactId>wsdl4j</artifactId>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<!-- maven-jaxb2-plugin 插件,用于实现将 WSDL 生成目标类 -->
51+
<plugin>
52+
<groupId>org.jvnet.jaxb2.maven2</groupId>
53+
<artifactId>maven-jaxb2-plugin</artifactId>
54+
<version>0.14.0</version>
55+
<executions>
56+
<execution>
57+
<goals>
58+
<goal>generate</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
<configuration>
63+
<!-- WSDL 源文件地址 -->
64+
<schemaLanguage>WSDL</schemaLanguage>
65+
<schemas>
66+
<schema>
67+
<url>http://127.0.0.1:8080/ws/users.wsdl</url>
68+
</schema>
69+
</schemas>
70+
<!-- 生成代码目标包 -->
71+
<generatePackage>cn.iocoder.springboot.lab65.demo.wsdl</generatePackage>
72+
</configuration>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.iocoder.springboot.lab65.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
// 启动 Spring Boot 应用
11+
SpringApplication.run(DemoApplication.class, args);
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.iocoder.springboot.lab65.demo.client;
2+
3+
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateRequest;
4+
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateResponse;
5+
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetRequest;
6+
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
7+
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
8+
9+
public class UserClient extends WebServiceGatewaySupport {
10+
11+
public UserGetResponse getUser(Integer id) {
12+
// 创建请求对象
13+
UserGetRequest request = new UserGetRequest();
14+
request.setId(id);
15+
// 执行请求
16+
return (UserGetResponse) getWebServiceTemplate().marshalSendAndReceive(request);
17+
}
18+
19+
public UserCreateResponse createUser(String name, Integer gender) {
20+
// 创建请求对象
21+
UserCreateRequest request = new UserCreateRequest();
22+
request.setName(name);
23+
request.setGender(gender);
24+
// 执行请求
25+
return (UserCreateResponse) getWebServiceTemplate().marshalSendAndReceive(request);
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.iocoder.springboot.lab65.demo.config;
2+
3+
import cn.iocoder.springboot.lab65.demo.client.UserClient;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
7+
8+
@Configuration
9+
public class WebServicesConfig {
10+
11+
// 创建 Jaxb2Marshaller Bean,实现 XML 与 Bean 的互相转换
12+
@Bean
13+
public Jaxb2Marshaller marshaller() {
14+
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
15+
marshaller.setContextPath("cn.iocoder.springboot.lab65.demo.wsdl"); // 用户服务的 WSDL 文件
16+
return marshaller;
17+
}
18+
19+
// 创建 UserClient Bean
20+
@Bean
21+
public UserClient countryClient(Jaxb2Marshaller marshaller) {
22+
UserClient client = new UserClient();
23+
client.setDefaultUri("http://127.0.0.1:8080/ws"); // 用户服务的 Web Services 地址
24+
client.setMarshaller(marshaller);
25+
client.setUnmarshaller(marshaller);
26+
return client;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)