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,43 @@
|
||||
<?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>lab-65-spring-ws-demo</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-65-cxf-ws-demo-user-service</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>
|
||||
<!-- 实现 CXF 对 Web Services 的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
|
||||
<version>3.3.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.iocoder.springboot.lab65.userservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UserServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 启动 Spring Boot 应用
|
||||
SpringApplication.run(UserServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.config;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.service.UserService;
|
||||
import org.apache.cxf.Bus;
|
||||
import org.apache.cxf.bus.spring.SpringBus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
@Configuration
|
||||
public class WebServicesConfig {
|
||||
|
||||
public static final String NAMESPACE_URI = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
|
||||
|
||||
@Bean(name = Bus.DEFAULT_BUS_ID)
|
||||
public SpringBus springBus() {
|
||||
return new SpringBus();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Endpoint endpoint(UserService userService) {
|
||||
Endpoint endpoint = Endpoint.create(userService);
|
||||
endpoint.publish("/user");//发布地址
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.request;
|
||||
|
||||
/**
|
||||
* 获得用户信息 Request
|
||||
*/
|
||||
public class UserGetRequest {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserGetRequest setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.response;
|
||||
|
||||
/**
|
||||
* 获得用户信息 Response
|
||||
*/
|
||||
public class UserGetResponse {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserGetResponse setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserGetResponse setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserGetResponse setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.service;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
@WebService(targetNamespace = WebServicesConfig.NAMESPACE_URI
|
||||
// ,
|
||||
// name = "userPortName"
|
||||
)
|
||||
public interface UserService {
|
||||
|
||||
UserGetResponse get(UserGetRequest request);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.service;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
@WebService(
|
||||
serviceName = "userService", // 服务名称
|
||||
targetNamespace = WebServicesConfig.NAMESPACE_URI, // WSDL 命名空间
|
||||
endpointInterface = "cn.iocoder.springboot.lab65.userservice.service.UserService" // Web Services 对应接口
|
||||
// name = "userPortType", // portType 名称,作为客户端生成代码时的接口名称 TODO
|
||||
// portName = "userPortName" // port 名称 TODO
|
||||
)
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public UserGetResponse get(UserGetRequest request) {
|
||||
UserGetResponse response = new UserGetResponse();
|
||||
response.setId(request.getId());
|
||||
response.setName("没有昵称:" + request.getId());
|
||||
response.setGender(request.getId() % 2 + 1);
|
||||
return response;
|
||||
}
|
||||
|
||||
// @PayloadRoot(namespace = WebServicesConfig.NAMESPACE_URI, localPart = "UserCreateRequest")
|
||||
// @ResponsePayload
|
||||
// public UserCreateResponse create(@RequestPayload UserCreateRequest request) {
|
||||
// UserCreateResponse response = new UserCreateResponse();
|
||||
// response.setId((int) (System.currentTimeMillis() / 1000));
|
||||
// return response;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# CXF 配置项,对应 CxfProperties 配置类
|
||||
cxf:
|
||||
path: /ws/ # CXF CXFServlet 的匹配路径
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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>lab-65-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>
|
||||
<!-- WSDL 源文件地址 -->
|
||||
<schemaLanguage>WSDL</schemaLanguage>
|
||||
<schemas>
|
||||
<schema>
|
||||
<url>http://127.0.0.1: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,28 @@
|
||||
package cn.iocoder.springboot.lab65.demo.client;
|
||||
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateRequest;
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateResponse;
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserGetResponse;
|
||||
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
|
||||
|
||||
public class UserClient extends WebServiceGatewaySupport {
|
||||
|
||||
public UserGetResponse getUser(Integer id) {
|
||||
// 创建请求对象
|
||||
UserGetRequest request = new UserGetRequest();
|
||||
request.setId(id);
|
||||
// 执行请求
|
||||
return (UserGetResponse) getWebServiceTemplate().marshalSendAndReceive(request);
|
||||
}
|
||||
|
||||
public UserCreateResponse createUser(String name, Integer gender) {
|
||||
// 创建请求对象
|
||||
UserCreateRequest request = new UserCreateRequest();
|
||||
request.setName(name);
|
||||
request.setGender(gender);
|
||||
// 执行请求
|
||||
return (UserCreateResponse) getWebServiceTemplate().marshalSendAndReceive(request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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 {
|
||||
|
||||
// 创建 Jaxb2Marshaller Bean,实现 XML 与 Bean 的互相转换
|
||||
@Bean
|
||||
public Jaxb2Marshaller marshaller() {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath("cn.iocoder.springboot.lab65.demo.wsdl"); // 用户服务的 WSDL 文件
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
// 创建 UserClient Bean
|
||||
@Bean
|
||||
public UserClient countryClient(Jaxb2Marshaller marshaller) {
|
||||
UserClient client = new UserClient();
|
||||
client.setDefaultUri("http://127.0.0.1:8080/ws"); // 用户服务的 Web Services 地址
|
||||
client.setMarshaller(marshaller);
|
||||
client.setUnmarshaller(marshaller);
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.iocoder.springboot.lab65.demo.controller;
|
||||
|
||||
import cn.iocoder.springboot.lab65.demo.client.UserClient;
|
||||
import cn.iocoder.springboot.lab65.demo.wsdl.UserCreateResponse;
|
||||
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) {
|
||||
// 执行 Web Services 请求
|
||||
UserCreateResponse response = userClient.createUser(name, gender);
|
||||
// 响应
|
||||
return response.getId();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
server:
|
||||
port: 9090
|
||||
20
lab-65/lab-65-cxf-ws-demo/pom.xml
Normal file
20
lab-65/lab-65-cxf-ws-demo/pom.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?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>lab-65</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-65-cxf-ws-demo</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-65-cxf-ws-demo-user-service</module>
|
||||
<!-- <module>lab-65-spring-ws-demo-application</module>-->
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -3,7 +3,7 @@
|
||||
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>
|
||||
<artifactId>lab-65-spring-ws-demo</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
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>
|
||||
<artifactId>lab-65-spring-ws-demo</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab4-64-spring-ws-demo</artifactId>
|
||||
<artifactId>lab-65-spring-ws-demo</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-65-spring-ws-demo-user-service</module>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<modules>
|
||||
<module>lab-65-spring-ws-demo</module>
|
||||
<module>lab-65-ws-feign-client</module>
|
||||
<module>lab-65-cxf-ws-demo</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user