Skip to content

Commit 1315127

Browse files
author
YunaiV
committed
开始 WebService 入门示例
1 parent c269671 commit 1315127

File tree

5 files changed

+122
-22
lines changed

5 files changed

+122
-22
lines changed

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

+40-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,50 @@
3232
</dependencyManagement>
3333

3434
<dependencies>
35-
<!-- 引入 Spring Boot 基础 Starter 依赖 -->
35+
<!-- &lt;!&ndash; 实现对 SpringMVC 的自动化配置 &ndash;&gt;-->
36+
<!-- <dependency>-->
37+
<!-- <groupId>org.springframework.boot</groupId>-->
38+
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
39+
<!-- </dependency>-->
40+
41+
<!-- 实现对 Spring WebService 的自动化配置 -->
3642
<dependency>
3743
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-starter</artifactId>
44+
<artifactId>spring-boot-starter-web-services</artifactId>
45+
</dependency>
46+
47+
<!-- tag::springws[] -->
48+
<dependency>
49+
<groupId>wsdl4j</groupId>
50+
<artifactId>wsdl4j</artifactId>
3951
</dependency>
52+
<!-- end::springws[] -->
4053
</dependencies>
4154

55+
<build>
56+
<plugins>
57+
<!-- tag::xsd[] -->
58+
<plugin>
59+
<groupId>org.codehaus.mojo</groupId>
60+
<artifactId>jaxb2-maven-plugin</artifactId>
61+
<version>2.5.0</version>
62+
<executions>
63+
<execution>
64+
<id>xjc</id>
65+
<goals>
66+
<goal>xjc</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
<configuration>
71+
<sources>
72+
<source>${project.basedir}/src/main/resources/users.xsd</source>
73+
</sources>
74+
<packageName>cn.iocoder.springboot.lab65.userservice.model</packageName>
75+
</configuration>
76+
</plugin>
77+
<!-- end::xsd[] -->
78+
</plugins>
79+
</build>
4280

4381
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.iocoder.springboot.lab65.userservice.config;
2+
3+
import org.springframework.boot.autoconfigure.webservices.WebServicesProperties;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.core.io.ClassPathResource;
7+
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
8+
import org.springframework.xml.xsd.SimpleXsdSchema;
9+
import org.springframework.xml.xsd.XsdSchema;
10+
11+
@Configuration
12+
public class WebServiceConfig {
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
17+
public XsdSchema usersSchema() {
18+
return new SimpleXsdSchema(new ClassPathResource("users.xsd"));
19+
}
20+
21+
@Bean(name = "users")
22+
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema usersSchema, WebServicesProperties properties) {
23+
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
24+
wsdl11Definition.setLocationUri(properties.getPath());
25+
wsdl11Definition.setTargetNamespace(NAMESPACE_URI);
26+
wsdl11Definition.setSchema(usersSchema);
27+
wsdl11Definition.setPortTypeName("UsersPort");
28+
return wsdl11Definition;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.iocoder.springboot.lab65.userservice.endpoint;
2+
3+
import cn.iocoder.springboot.lab65.userservice.config.WebServiceConfig;
4+
import cn.iocoder.springboot.lab65.userservice.model.UserGetRequest;
5+
import cn.iocoder.springboot.lab65.userservice.model.UserGetResponse;
6+
import org.springframework.ws.server.endpoint.annotation.Endpoint;
7+
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
8+
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
9+
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
10+
11+
@Endpoint
12+
public class UserEndpoint {
13+
14+
@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserGetRequest")
15+
@ResponsePayload
16+
public UserGetResponse get(@RequestPayload UserGetRequest request) {
17+
UserGetResponse response = new UserGetResponse();
18+
response.setId(request.getId());
19+
response.setName("没有昵称:" + request.getId());
20+
response.setGender(request.getId() % 2 + 1);
21+
return response;
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring:
2+
webservices:
3+
path: /ws
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
1-
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service"
2-
targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified">
1+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
2+
targetNamespace="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo"
3+
elementFormDefault="qualified">
34

4-
<xs:element name="getCountryRequest">
5+
<xs:element name="UserGetRequest">
56
<xs:complexType>
67
<xs:sequence>
8+
<xs:element name="id" type="xs:int"/>
9+
</xs:sequence>
10+
</xs:complexType>
11+
</xs:element>
12+
13+
<xs:element name="UserGetResponse">
14+
<xs:complexType>
15+
<xs:sequence>
16+
<xs:element name="id" type="xs:int" />
717
<xs:element name="name" type="xs:string"/>
18+
<xs:element name="gender" type="xs:int"/>
819
</xs:sequence>
920
</xs:complexType>
1021
</xs:element>
1122

12-
<xs:element name="getCountryResponse">
23+
<xs:element name="UserCreateRequest">
1324
<xs:complexType>
1425
<xs:sequence>
15-
<xs:element name="country" type="tns:country"/>
26+
<xs:element name="name" type="xs:string"/>
27+
<xs:element name="gender" type="xs:int"/>
1628
</xs:sequence>
1729
</xs:complexType>
1830
</xs:element>
1931

20-
<xs:complexType name="country">
21-
<xs:sequence>
22-
<xs:element name="name" type="xs:string"/>
23-
<xs:element name="population" type="xs:int"/>
24-
<xs:element name="capital" type="xs:string"/>
25-
<xs:element name="currency" type="tns:currency"/>
26-
</xs:sequence>
27-
</xs:complexType>
32+
<xs:element name="UserCreateResponse">
33+
<xs:complexType>
34+
<xs:sequence>
35+
<xs:element name="id" type="xs:int"/>
36+
</xs:sequence>
37+
</xs:complexType>
38+
</xs:element>
2839

29-
<xs:simpleType name="currency">
30-
<xs:restriction base="xs:string">
31-
<xs:enumeration value="GBP"/>
32-
<xs:enumeration value="EUR"/>
33-
<xs:enumeration value="PLN"/>
34-
</xs:restriction>
35-
</xs:simpleType>
3640
</xs:schema>

0 commit comments

Comments
 (0)