mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
开始 WebService 入门示例
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lab4-64-spring-ws-demo</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-65-spring-ws-demo-application</artifactId>
|
||||
|
||||
<properties>
|
||||
<!-- 依赖相关配置 -->
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<!-- 插件相关配置 -->
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring WebService 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web-services</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Java WSDL 实现库 -->
|
||||
<dependency>
|
||||
<groupId>wsdl4j</groupId>
|
||||
<artifactId>wsdl4j</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- maven-jaxb2-plugin 插件,用于实现将 WSDL 生成目标类 -->
|
||||
<plugin>
|
||||
<groupId>org.jvnet.jaxb2.maven2</groupId>
|
||||
<artifactId>maven-jaxb2-plugin</artifactId>
|
||||
<version>0.14.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<schemaLanguage>WSDL</schemaLanguage>
|
||||
<schemas>
|
||||
<schema>
|
||||
<url>http://localhost:8080/ws/users.wsdl</url>
|
||||
</schema>
|
||||
</schemas>
|
||||
<generatePackage>cn.iocoder.springboot.lab65.demo.wsdl</generatePackage>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.iocoder.springboot.lab65.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 启动 Spring Boot 应用
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.iocoder.springboot.lab65.demo.client;
|
||||
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
|
||||
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
|
||||
import org.springframework.ws.soap.client.core.SoapActionCallback;
|
||||
|
||||
public class UserClient extends WebServiceGatewaySupport {
|
||||
|
||||
public static final String WEB_SERVICES_URI = "http://127.0.0.1:8080/ws";
|
||||
|
||||
private static final String WE_SERVICES_NAMESPACE = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
|
||||
|
||||
public UserGetResponse getUser(Integer id) {
|
||||
UserGetRequest request = new UserGetRequest();
|
||||
request.setId(id);
|
||||
return (UserGetResponse) getWebServiceTemplate().marshalSendAndReceive(
|
||||
request, new SoapActionCallback(WE_SERVICES_NAMESPACE + "/UserCreateRequest"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springboot.lab65.demo.config;
|
||||
|
||||
import cn.iocoder.springboot.lab65.demo.client.UserClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
|
||||
@Configuration
|
||||
public class WebServicesConfig {
|
||||
|
||||
@Bean
|
||||
public Jaxb2Marshaller marshaller() {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath("cn.iocoder.springboot.lab65.demo.wsdl");
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UserClient countryClient(Jaxb2Marshaller marshaller) {
|
||||
UserClient client = new UserClient();
|
||||
client.setDefaultUri(UserClient.WEB_SERVICES_URI);
|
||||
client.setMarshaller(marshaller);
|
||||
client.setUnmarshaller(marshaller);
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.iocoder.springboot.lab65.demo.controller;
|
||||
|
||||
import cn.iocoder.springboot.lab65.demo.client.UserClient;
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
private UserClient userClient;
|
||||
|
||||
@GetMapping("/get")
|
||||
public String get(@RequestParam("id") Integer id) {
|
||||
// 执行 Web Services 请求
|
||||
UserGetResponse response = userClient.getUser(id);
|
||||
// 响应
|
||||
return response.getName();
|
||||
}
|
||||
|
||||
// @GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
|
||||
// public Integer create(@RequestParam("name") String name,
|
||||
// @RequestParam("gender") Integer gender) {
|
||||
// // 创建请求
|
||||
// UserCreateRequest request = UserCreateRequest.newBuilder()
|
||||
// .setName(name).setGender(gender).build();
|
||||
// // 执行 gRPC 请求
|
||||
// UserCreateResponse response = userServiceGrpc.create(request);
|
||||
// // 响应
|
||||
// return response.getId();
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
server:
|
||||
port: 9090
|
||||
@@ -13,7 +13,7 @@ import org.springframework.xml.xsd.XsdSchema;
|
||||
|
||||
@Configuration
|
||||
@EnableWs // 开启 Web Services 服务
|
||||
public class WebServiceConfig {
|
||||
public class WebServicesConfig {
|
||||
|
||||
public static final String NAMESPACE_URI = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.endpoint;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServiceConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserCreateRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserCreateResponse;
|
||||
import cn.iocoder.springboot.lab65.userservice.model.UserGetRequest;
|
||||
@@ -13,7 +13,7 @@ import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
|
||||
@Endpoint
|
||||
public class UserEndpoint {
|
||||
|
||||
@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserGetRequest")
|
||||
@PayloadRoot(namespace = WebServicesConfig.NAMESPACE_URI, localPart = "UserGetRequest")
|
||||
@ResponsePayload
|
||||
public UserGetResponse get(@RequestPayload UserGetRequest request) {
|
||||
UserGetResponse response = new UserGetResponse();
|
||||
@@ -23,7 +23,7 @@ public class UserEndpoint {
|
||||
return response;
|
||||
}
|
||||
|
||||
@PayloadRoot(namespace = WebServiceConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
|
||||
@PayloadRoot(namespace = WebServicesConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
|
||||
@ResponsePayload
|
||||
public UserCreateResponse create(@RequestPayload UserCreateRequest request) {
|
||||
UserCreateResponse response = new UserCreateResponse();
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-65-spring-ws-demo-user-service</module>
|
||||
<module>lab-65-spring-ws-demo-application</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user