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,29 +32,22 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- <!– 实现对 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-web-services</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- tag::springws[] -->
|
||||
<!-- Java WSDL 实现库 -->
|
||||
<dependency>
|
||||
<groupId>wsdl4j</groupId>
|
||||
<artifactId>wsdl4j</artifactId>
|
||||
</dependency>
|
||||
<!-- end::springws[] -->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- tag::xsd[] -->
|
||||
<!-- jaxb2-maven-plugin 插件,用于实现将 XML 生成目标类 -->
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||
@@ -68,13 +61,14 @@
|
||||
</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>
|
||||
|
||||
|
||||
@@ -1,31 +1,43 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.webservices.WebServicesProperties;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.ws.config.annotation.EnableWs;
|
||||
import org.springframework.ws.transport.http.MessageDispatcherServlet;
|
||||
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
|
||||
import org.springframework.xml.xsd.SimpleXsdSchema;
|
||||
import org.springframework.xml.xsd.XsdSchema;
|
||||
|
||||
@Configuration
|
||||
@EnableWs // 开启 Web Services 服务
|
||||
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 ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
|
||||
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
|
||||
servlet.setApplicationContext(applicationContext);
|
||||
servlet.setTransformWsdlLocations(true);
|
||||
return new ServletRegistrationBean<>(servlet, "/ws/*");
|
||||
}
|
||||
|
||||
@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);
|
||||
@Bean(name = "users")
|
||||
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema usersSchema) {
|
||||
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
|
||||
wsdl11Definition.setLocationUri("/ws");
|
||||
wsdl11Definition.setTargetNamespace(NAMESPACE_URI);
|
||||
wsdl11Definition.setSchema(usersSchema);
|
||||
wsdl11Definition.setPortTypeName("UsersPort");
|
||||
return wsdl11Definition;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.endpoint;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServiceConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserCreateRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserCreateResponse;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserGetResponse;
|
||||
import org.springframework.ws.server.endpoint.annotation.Endpoint;
|
||||
@@ -21,4 +23,12 @@ public class UserEndpoint {
|
||||
return response;
|
||||
}
|
||||
|
||||
@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
|
||||
@ResponsePayload
|
||||
public UserCreateResponse create(@RequestPayload UserCreateRequest request) {
|
||||
UserCreateResponse response = new UserCreateResponse();
|
||||
response.setId((int) (System.currentTimeMillis() / 1000));
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
spring:
|
||||
webservices:
|
||||
path: /ws
|
||||
Reference in New Issue
Block a user