Skip to content

Commit

Permalink
开始 WebService 入门示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jun 15, 2020
1 parent c269671 commit 1315127
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,50 @@
</dependencyManagement>

<dependencies>
<!-- 引入 Spring Boot 基础 Starter 依赖 -->
<!-- &lt;!&ndash; 实现对 SpringMVC 的自动化配置 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->

<!-- 实现对 Spring WebService 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<!-- tag::springws[] -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<!-- end::springws[] -->
</dependencies>

<build>
<plugins>
<!-- tag::xsd[] -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/users.xsd</source>
</sources>
<packageName>cn.iocoder.springboot.lab65.userservice.model</packageName>
</configuration>
</plugin>
<!-- end::xsd[] -->
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cn.iocoder.springboot.lab65.userservice.config;

import org.springframework.boot.autoconfigure.webservices.WebServicesProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@Configuration
public class WebServiceConfig {

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

@Bean
public XsdSchema usersSchema() {
return new SimpleXsdSchema(new ClassPathResource("users.xsd"));
}

@Bean(name = "users")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema usersSchema, WebServicesProperties properties) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setLocationUri(properties.getPath());
wsdl11Definition.setTargetNamespace(NAMESPACE_URI);
wsdl11Definition.setSchema(usersSchema);
wsdl11Definition.setPortTypeName("UsersPort");
return wsdl11Definition;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.iocoder.springboot.lab65.userservice.endpoint;

import cn.iocoder.springboot.lab65.userservice.config.WebServiceConfig;
import cn.iocoder.springboot.lab65.userservice.model.UserGetRequest;
import cn.iocoder.springboot.lab65.userservice.model.UserGetResponse;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class UserEndpoint {

@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserGetRequest")
@ResponsePayload
public UserGetResponse get(@RequestPayload UserGetRequest request) {
UserGetResponse response = new UserGetResponse();
response.setId(request.getId());
response.setName("没有昵称:" + request.getId());
response.setGender(request.getId() % 2 + 1);
return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
webservices:
path: /ws
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service"
targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo"
elementFormDefault="qualified">

<xs:element name="getCountryRequest">
<xs:element name="UserGetRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="UserGetResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="name" type="xs:string"/>
<xs:element name="gender" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="getCountryResponse">
<xs:element name="UserCreateRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="tns:country"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="gender" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="country">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="population" type="xs:int"/>
<xs:element name="capital" type="xs:string"/>
<xs:element name="currency" type="tns:currency"/>
</xs:sequence>
</xs:complexType>
<xs:element name="UserCreateResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleType name="currency">
<xs:restriction base="xs:string">
<xs:enumeration value="GBP"/>
<xs:enumeration value="EUR"/>
<xs:enumeration value="PLN"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

0 comments on commit 1315127

Please sign in to comment.