Skip to content

Commit 7fc06d7

Browse files
author
YunaiV
committed
开始 WebService 入门示例
1 parent 65bbd8a commit 7fc06d7

File tree

9 files changed

+182
-4
lines changed

9 files changed

+182
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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>lab4-64-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+
<schemaLanguage>WSDL</schemaLanguage>
64+
<schemas>
65+
<schema>
66+
<url>http://localhost:8080/ws/users.wsdl</url>
67+
</schema>
68+
</schemas>
69+
<generatePackage>cn.iocoder.springboot.lab65.demo.wsdl</generatePackage>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
</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,21 @@
1+
package cn.iocoder.springboot.lab65.demo.client;
2+
3+
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetRequest;
4+
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
5+
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
6+
import org.springframework.ws.soap.client.core.SoapActionCallback;
7+
8+
public class UserClient extends WebServiceGatewaySupport {
9+
10+
public static final String WEB_SERVICES_URI = "http://127.0.0.1:8080/ws";
11+
12+
private static final String WE_SERVICES_NAMESPACE = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
13+
14+
public UserGetResponse getUser(Integer id) {
15+
UserGetRequest request = new UserGetRequest();
16+
request.setId(id);
17+
return (UserGetResponse) getWebServiceTemplate().marshalSendAndReceive(
18+
request, new SoapActionCallback(WE_SERVICES_NAMESPACE + "/UserCreateRequest"));
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
@Bean
12+
public Jaxb2Marshaller marshaller() {
13+
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
14+
marshaller.setContextPath("cn.iocoder.springboot.lab65.demo.wsdl");
15+
return marshaller;
16+
}
17+
18+
@Bean
19+
public UserClient countryClient(Jaxb2Marshaller marshaller) {
20+
UserClient client = new UserClient();
21+
client.setDefaultUri(UserClient.WEB_SERVICES_URI);
22+
client.setMarshaller(marshaller);
23+
client.setUnmarshaller(marshaller);
24+
return client;
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.iocoder.springboot.lab65.demo.controller;
2+
3+
import cn.iocoder.springboot.lab65.demo.client.UserClient;
4+
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/demo")
13+
public class DemoController {
14+
15+
@Autowired
16+
private UserClient userClient;
17+
18+
@GetMapping("/get")
19+
public String get(@RequestParam("id") Integer id) {
20+
// 执行 Web Services 请求
21+
UserGetResponse response = userClient.getUser(id);
22+
// 响应
23+
return response.getName();
24+
}
25+
26+
// @GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
27+
// public Integer create(@RequestParam("name") String name,
28+
// @RequestParam("gender") Integer gender) {
29+
// // 创建请求
30+
// UserCreateRequest request = UserCreateRequest.newBuilder()
31+
// .setName(name).setGender(gender).build();
32+
// // 执行 gRPC 请求
33+
// UserCreateResponse response = userServiceGrpc.create(request);
34+
// // 响应
35+
// return response.getId();
36+
// }
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server:
2+
port: 9090
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@Configuration
1515
@EnableWs // 开启 Web Services 服务
16-
public class WebServiceConfig {
16+
public class WebServicesConfig {
1717

1818
public static final String NAMESPACE_URI = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
1919

lab-65/lab-65-spring-ws-demo/lab-65-spring-ws-demo-user-service/src/main/java/cn/iocoder/springboot/lab65/userservice/endpoint/UserEndpoint.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cn.iocoder.springboot.lab65.userservice.endpoint;
22

3-
import cn.iocoder.springboot.lab65.userservice.config.WebServiceConfig;
3+
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
44
import cn.iocoder.springboot.lab65.userservice.model.UserCreateRequest;
55
import cn.iocoder.springboot.lab65.userservice.model.UserCreateResponse;
66
import cn.iocoder.springboot.lab65.userservice.model.UserGetRequest;
@@ -13,7 +13,7 @@
1313
@Endpoint
1414
public class UserEndpoint {
1515

16-
@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserGetRequest")
16+
@PayloadRoot(namespace = WebServicesConfig.NAMESPACE_URI, localPart = "UserGetRequest")
1717
@ResponsePayload
1818
public UserGetResponse get(@RequestPayload UserGetRequest request) {
1919
UserGetResponse response = new UserGetResponse();
@@ -23,7 +23,7 @@ public UserGetResponse get(@RequestPayload UserGetRequest request) {
2323
return response;
2424
}
2525

26-
@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
26+
@PayloadRoot(namespace = WebServicesConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
2727
@ResponsePayload
2828
public UserCreateResponse create(@RequestPayload UserCreateRequest request) {
2929
UserCreateResponse response = new UserCreateResponse();

lab-65/lab-65-spring-ws-demo/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<packaging>pom</packaging>
1414
<modules>
1515
<module>lab-65-spring-ws-demo-user-service</module>
16+
<module>lab-65-spring-ws-demo-application</module>
1617
</modules>
1718

1819

0 commit comments

Comments
 (0)