mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
开始 WebService 入门示例
This commit is contained in:
@@ -32,12 +32,50 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Boot 基础 Starter 依赖 -->
|
||||
<!-- <!– 实现对 SpringMVC 的自动化配置 –>-->
|
||||
<!-- <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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring:
|
||||
webservices:
|
||||
path: /ws
|
||||
@@ -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="UserCreateRequest">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<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="UserCreateResponse">
|
||||
<xs:complexType>
|
||||
<xs:sequence>
|
||||
<xs:element name="country" type="tns:country"/>
|
||||
<xs:element name="id" 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: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>
|
||||
|
||||
Reference in New Issue
Block a user