Skip to content

Commit 0c95e44

Browse files
author
YunaiV
committed
开始 WebService 入门示例
1 parent 87639c6 commit 0c95e44

File tree

11 files changed

+143
-64
lines changed

11 files changed

+143
-64
lines changed

lab-65/lab-65-cxf-ws-demo/lab-65-cxf-ws-demo-application/pom.xml

+4-25
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
<build>
4444
<plugins>
45+
<!-- cxf-codegen-plugin 插件,用于实现将 WSDL 生成目标类 -->
4546
<plugin>
4647
<groupId>org.apache.cxf</groupId>
4748
<artifactId>cxf-codegen-plugin</artifactId>
@@ -51,44 +52,22 @@
5152
<id>generate-sources</id>
5253
<phase>generate-sources</phase>
5354
<configuration>
54-
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
55+
<!-- WSDL 源文件地址 -->
5556
<wsdlOptions>
5657
<wsdlOption>
5758
<wsdl>src/main/resources/wsdl/user.wsdl</wsdl>
5859
<wsdlLocation>classpath:wsdl/user.wsdl</wsdlLocation>
5960
</wsdlOption>
6061
</wsdlOptions>
62+
<!-- 生成 Java 代码目录 -->
63+
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
6164
</configuration>
6265
<goals>
6366
<goal>wsdl2java</goal>
6467
</goals>
6568
</execution>
6669
</executions>
6770
</plugin>
68-
<!-- &lt;!&ndash; maven-jaxb2-plugin 插件,用于实现将 WSDL 生成目标类 &ndash;&gt;-->
69-
<!-- <plugin>-->
70-
<!-- <groupId>org.jvnet.jaxb2.maven2</groupId>-->
71-
<!-- <artifactId>maven-jaxb2-plugin</artifactId>-->
72-
<!-- <version>0.14.0</version>-->
73-
<!-- <executions>-->
74-
<!-- <execution>-->
75-
<!-- <goals>-->
76-
<!-- <goal>generate</goal>-->
77-
<!-- </goals>-->
78-
<!-- </execution>-->
79-
<!-- </executions>-->
80-
<!-- <configuration>-->
81-
<!-- &lt;!&ndash; WSDL 源文件地址 &ndash;&gt;-->
82-
<!-- <schemaLanguage>WSDL</schemaLanguage>-->
83-
<!-- <schemas>-->
84-
<!-- <schema>-->
85-
<!-- <url>http://127.0.0.1:8080/ws/users.wsdl</url>-->
86-
<!-- </schema>-->
87-
<!-- </schemas>-->
88-
<!-- &lt;!&ndash; 生成代码目标包 &ndash;&gt;-->
89-
<!-- <generatePackage>cn.iocoder.springboot.lab65.demo.wsdl</generatePackage>-->
90-
<!-- </configuration>-->
91-
<!-- </plugin>-->
9271
</plugins>
9372
</build>
9473

Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import org.springframework.context.annotation.Configuration;
77

88
@Configuration
9-
public class WebServicesConfig {
9+
public class CXFConfig {
1010

1111
@Bean
1212
public UserService userService() {
1313
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
14+
// 设置 UserService 接口
1415
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
15-
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:8080/ws/user");
16+
// 设置 Web Services 地址
17+
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:9090/ws/user");
18+
// 创建
1619
return (UserService) jaxWsProxyFactoryBean.create();
1720
}
1821

lab-65/lab-65-cxf-ws-demo/lab-65-cxf-ws-demo-application/src/main/java/cn/iocoder/springboot/lab65/demo/controller/DemoController.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package cn.iocoder.springboot.lab65.demo.controller;
22

3-
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserGetRequest;
4-
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserGetResponse;
5-
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserService;
3+
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.*;
64
import org.springframework.beans.factory.annotation.Autowired;
75
import org.springframework.web.bind.annotation.GetMapping;
86
import org.springframework.web.bind.annotation.RequestMapping;
@@ -26,17 +24,17 @@ public String get(@RequestParam("id") Integer id) {
2624
return response.getName();
2725
}
2826

29-
// @GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
30-
// public Integer create(@RequestParam("name") String name,
31-
// @RequestParam("gender") Integer gender) {
32-
// // 请求
33-
// UserCreateRequest request = new UserCreateRequest();
34-
// request.setName(name);
35-
// request.setGender(gender);
36-
// // 执行 Web Services 请求
37-
// UserCreateResponse response = userClient.createUser(name, gender);
38-
// // 响应
39-
// return response.getId();
40-
// }
27+
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
28+
public Integer create(@RequestParam("name") String name,
29+
@RequestParam("gender") Integer gender) {
30+
// 请求
31+
UserCreateRequest request = new UserCreateRequest();
32+
request.setName(name);
33+
request.setGender(gender);
34+
// 执行 Web Services 请求
35+
UserCreateResponse response = userService.create(request);
36+
// 响应
37+
return response.getId();
38+
}
4139

4240
}

lab-65/lab-65-cxf-ws-demo/lab-65-cxf-ws-demo-application/src/main/resources/application.yml

-2
This file was deleted.

lab-65/lab-65-cxf-ws-demo/lab-65-cxf-ws-demo-application/src/main/resources/wsdl/user.wsdl

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<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">
22
<wsdl:types>
33
<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">
4+
<xs:element name="create" type="tns:create"/>
5+
<xs:element name="createResponse" type="tns:createResponse"/>
46
<xs:element name="get" type="tns:get"/>
57
<xs:element name="getResponse" type="tns:getResponse"/>
68
<xs:complexType name="get">
@@ -25,6 +27,27 @@
2527
<xs:element minOccurs="0" name="name" type="xs:string"/>
2628
</xs:sequence>
2729
</xs:complexType>
30+
<xs:complexType name="create">
31+
<xs:sequence>
32+
<xs:element minOccurs="0" name="arg0" type="tns:userCreateRequest"/>
33+
</xs:sequence>
34+
</xs:complexType>
35+
<xs:complexType name="userCreateRequest">
36+
<xs:sequence>
37+
<xs:element minOccurs="0" name="gender" type="xs:int"/>
38+
<xs:element minOccurs="0" name="name" type="xs:string"/>
39+
</xs:sequence>
40+
</xs:complexType>
41+
<xs:complexType name="createResponse">
42+
<xs:sequence>
43+
<xs:element minOccurs="0" name="return" type="tns:userCreateResponse"/>
44+
</xs:sequence>
45+
</xs:complexType>
46+
<xs:complexType name="userCreateResponse">
47+
<xs:sequence>
48+
<xs:element minOccurs="0" name="id" type="xs:int"/>
49+
</xs:sequence>
50+
</xs:complexType>
2851
</xs:schema>
2952
</wsdl:types>
3053
<wsdl:message name="getResponse">
@@ -33,11 +56,21 @@
3356
<wsdl:message name="get">
3457
<wsdl:part element="tns:get" name="parameters"> </wsdl:part>
3558
</wsdl:message>
59+
<wsdl:message name="create">
60+
<wsdl:part element="tns:create" name="parameters"> </wsdl:part>
61+
</wsdl:message>
62+
<wsdl:message name="createResponse">
63+
<wsdl:part element="tns:createResponse" name="parameters"> </wsdl:part>
64+
</wsdl:message>
3665
<wsdl:portType name="UserService">
3766
<wsdl:operation name="get">
3867
<wsdl:input message="tns:get" name="get"> </wsdl:input>
3968
<wsdl:output message="tns:getResponse" name="getResponse"> </wsdl:output>
4069
</wsdl:operation>
70+
<wsdl:operation name="create">
71+
<wsdl:input message="tns:create" name="create"> </wsdl:input>
72+
<wsdl:output message="tns:createResponse" name="createResponse"> </wsdl:output>
73+
</wsdl:operation>
4174
</wsdl:portType>
4275
<wsdl:binding name="userServiceSoapBinding" type="tns:UserService">
4376
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
@@ -50,6 +83,15 @@
5083
<soap:body use="literal"/>
5184
</wsdl:output>
5285
</wsdl:operation>
86+
<wsdl:operation name="create">
87+
<soap:operation soapAction="" style="document"/>
88+
<wsdl:input name="create">
89+
<soap:body use="literal"/>
90+
</wsdl:input>
91+
<wsdl:output name="createResponse">
92+
<soap:body use="literal"/>
93+
</wsdl:output>
94+
</wsdl:operation>
5395
</wsdl:binding>
5496
<wsdl:service name="userService">
5597
<wsdl:port binding="tns:userServiceSoapBinding" name="UserServiceImplPort">
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import javax.xml.ws.Endpoint;
1010

1111
@Configuration
12-
public class WebServicesConfig {
12+
public class CXFConfig {
1313

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

@@ -19,7 +19,7 @@ public SpringBus springBus() {
1919
}
2020

2121
@Bean
22-
public Endpoint endpoint(UserService userService) {
22+
public Endpoint userServiceEndpoint(UserService userService) {
2323
Endpoint endpoint = Endpoint.create(userService);
2424
endpoint.publish("/user");//发布地址
2525
return endpoint;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springboot.lab65.userservice.request;
2+
3+
/**
4+
* 创建用户信息 Request
5+
*/
6+
public class UserCreateRequest {
7+
8+
/**
9+
* 昵称
10+
*/
11+
private String name;
12+
/**
13+
* 性别别
14+
*/
15+
private Integer gender;
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public UserCreateRequest setName(String name) {
22+
this.name = name;
23+
return this;
24+
}
25+
26+
public Integer getGender() {
27+
return gender;
28+
}
29+
30+
public UserCreateRequest setGender(Integer gender) {
31+
this.gender = gender;
32+
return this;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.iocoder.springboot.lab65.userservice.response;
2+
3+
/**
4+
* 创建用户信息 Response
5+
*/
6+
public class UserCreateResponse {
7+
8+
/**
9+
* 用户编号
10+
*/
11+
private Integer id;
12+
13+
public Integer getId() {
14+
return id;
15+
}
16+
17+
public UserCreateResponse setId(Integer id) {
18+
this.id = id;
19+
return this;
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package cn.iocoder.springboot.lab65.userservice.service;
22

3-
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
3+
import cn.iocoder.springboot.lab65.userservice.config.CXFConfig;
4+
import cn.iocoder.springboot.lab65.userservice.request.UserCreateRequest;
45
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
6+
import cn.iocoder.springboot.lab65.userservice.response.UserCreateResponse;
57
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
68

79
import javax.jws.WebService;
810

9-
@WebService(targetNamespace = WebServicesConfig.NAMESPACE_URI
10-
// ,
11-
// name = "userPortName"
12-
)
11+
@WebService(targetNamespace = CXFConfig.NAMESPACE_URI)
1312
public interface UserService {
1413

1514
UserGetResponse get(UserGetRequest request);
1615

16+
UserCreateResponse create(UserCreateRequest request);
17+
1718
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package cn.iocoder.springboot.lab65.userservice.service;
22

3-
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
3+
import cn.iocoder.springboot.lab65.userservice.config.CXFConfig;
4+
import cn.iocoder.springboot.lab65.userservice.request.UserCreateRequest;
45
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
6+
import cn.iocoder.springboot.lab65.userservice.response.UserCreateResponse;
57
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
68
import org.springframework.stereotype.Service;
79

810
import javax.jws.WebService;
911

12+
@Service
1013
@WebService(
1114
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
15+
targetNamespace = CXFConfig.NAMESPACE_URI // WSDL 命名空间
1616
)
17-
@Service
1817
public class UserServiceImpl implements UserService {
1918

2019
@Override
@@ -26,12 +25,11 @@ public UserGetResponse get(UserGetRequest request) {
2625
return response;
2726
}
2827

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-
// }
28+
@Override
29+
public UserCreateResponse create(UserCreateRequest request) {
30+
UserCreateResponse response = new UserCreateResponse();
31+
response.setId((int) (System.currentTimeMillis() / 1000));
32+
return response;
33+
}
3634

3735
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# CXF 配置项,对应 CxfProperties 配置类
22
cxf:
33
path: /ws/ # CXF CXFServlet 的匹配路径
4+
5+
server:
6+
port: 9090 # 设置服务器端口为 9090

0 commit comments

Comments
 (0)