mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
开始 WebService 入门示例
This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- cxf-codegen-plugin 插件,用于实现将 WSDL 生成目标类 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-codegen-plugin</artifactId>
|
||||
@@ -51,13 +52,15 @@
|
||||
<id>generate-sources</id>
|
||||
<phase>generate-sources</phase>
|
||||
<configuration>
|
||||
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
|
||||
<!-- WSDL 源文件地址 -->
|
||||
<wsdlOptions>
|
||||
<wsdlOption>
|
||||
<wsdl>src/main/resources/wsdl/user.wsdl</wsdl>
|
||||
<wsdlLocation>classpath:wsdl/user.wsdl</wsdlLocation>
|
||||
</wsdlOption>
|
||||
</wsdlOptions>
|
||||
<!-- 生成 Java 代码目录 -->
|
||||
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>wsdl2java</goal>
|
||||
@@ -65,30 +68,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- <!– 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>
|
||||
|
||||
|
||||
@@ -6,13 +6,16 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class WebServicesConfig {
|
||||
public class CXFConfig {
|
||||
|
||||
@Bean
|
||||
public UserService userService() {
|
||||
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
|
||||
// 设置 UserService 接口
|
||||
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
|
||||
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:8080/ws/user");
|
||||
// 设置 Web Services 地址
|
||||
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:9090/ws/user");
|
||||
// 创建
|
||||
return (UserService) jaxWsProxyFactoryBean.create();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cn.iocoder.springboot.lab65.demo.controller;
|
||||
|
||||
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserGetRequest;
|
||||
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserGetResponse;
|
||||
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.UserService;
|
||||
import https.github_com.yunaiv.springboot_labs.tree.master.lab_65.lab_65_spring_ws_demo.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -26,17 +24,17 @@ public class DemoController {
|
||||
return response.getName();
|
||||
}
|
||||
|
||||
// @GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
|
||||
// public Integer create(@RequestParam("name") String name,
|
||||
// @RequestParam("gender") Integer gender) {
|
||||
// // 请求
|
||||
// UserCreateRequest request = new UserCreateRequest();
|
||||
// request.setName(name);
|
||||
// request.setGender(gender);
|
||||
// // 执行 Web Services 请求
|
||||
// UserCreateResponse response = userClient.createUser(name, gender);
|
||||
// // 响应
|
||||
// return response.getId();
|
||||
// }
|
||||
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
|
||||
public Integer create(@RequestParam("name") String name,
|
||||
@RequestParam("gender") Integer gender) {
|
||||
// 请求
|
||||
UserCreateRequest request = new UserCreateRequest();
|
||||
request.setName(name);
|
||||
request.setGender(gender);
|
||||
// 执行 Web Services 请求
|
||||
UserCreateResponse response = userService.create(request);
|
||||
// 响应
|
||||
return response.getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
server:
|
||||
port: 9090
|
||||
@@ -1,6 +1,8 @@
|
||||
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="userService" targetNamespace="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo">
|
||||
<wsdl:types>
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo" elementFormDefault="unqualified" targetNamespace="https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo" version="1.0">
|
||||
<xs:element name="create" type="tns:create"/>
|
||||
<xs:element name="createResponse" type="tns:createResponse"/>
|
||||
<xs:element name="get" type="tns:get"/>
|
||||
<xs:element name="getResponse" type="tns:getResponse"/>
|
||||
<xs:complexType name="get">
|
||||
@@ -25,6 +27,27 @@
|
||||
<xs:element minOccurs="0" name="name" type="xs:string"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
<xs:complexType name="create">
|
||||
<xs:sequence>
|
||||
<xs:element minOccurs="0" name="arg0" type="tns:userCreateRequest"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
<xs:complexType name="userCreateRequest">
|
||||
<xs:sequence>
|
||||
<xs:element minOccurs="0" name="gender" type="xs:int"/>
|
||||
<xs:element minOccurs="0" name="name" type="xs:string"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
<xs:complexType name="createResponse">
|
||||
<xs:sequence>
|
||||
<xs:element minOccurs="0" name="return" type="tns:userCreateResponse"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
<xs:complexType name="userCreateResponse">
|
||||
<xs:sequence>
|
||||
<xs:element minOccurs="0" name="id" type="xs:int"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:schema>
|
||||
</wsdl:types>
|
||||
<wsdl:message name="getResponse">
|
||||
@@ -33,11 +56,21 @@
|
||||
<wsdl:message name="get">
|
||||
<wsdl:part element="tns:get" name="parameters"> </wsdl:part>
|
||||
</wsdl:message>
|
||||
<wsdl:message name="create">
|
||||
<wsdl:part element="tns:create" name="parameters"> </wsdl:part>
|
||||
</wsdl:message>
|
||||
<wsdl:message name="createResponse">
|
||||
<wsdl:part element="tns:createResponse" name="parameters"> </wsdl:part>
|
||||
</wsdl:message>
|
||||
<wsdl:portType name="UserService">
|
||||
<wsdl:operation name="get">
|
||||
<wsdl:input message="tns:get" name="get"> </wsdl:input>
|
||||
<wsdl:output message="tns:getResponse" name="getResponse"> </wsdl:output>
|
||||
</wsdl:operation>
|
||||
<wsdl:operation name="create">
|
||||
<wsdl:input message="tns:create" name="create"> </wsdl:input>
|
||||
<wsdl:output message="tns:createResponse" name="createResponse"> </wsdl:output>
|
||||
</wsdl:operation>
|
||||
</wsdl:portType>
|
||||
<wsdl:binding name="userServiceSoapBinding" type="tns:UserService">
|
||||
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
|
||||
@@ -50,6 +83,15 @@
|
||||
<soap:body use="literal"/>
|
||||
</wsdl:output>
|
||||
</wsdl:operation>
|
||||
<wsdl:operation name="create">
|
||||
<soap:operation soapAction="" style="document"/>
|
||||
<wsdl:input name="create">
|
||||
<soap:body use="literal"/>
|
||||
</wsdl:input>
|
||||
<wsdl:output name="createResponse">
|
||||
<soap:body use="literal"/>
|
||||
</wsdl:output>
|
||||
</wsdl:operation>
|
||||
</wsdl:binding>
|
||||
<wsdl:service name="userService">
|
||||
<wsdl:port binding="tns:userServiceSoapBinding" name="UserServiceImplPort">
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
@Configuration
|
||||
public class WebServicesConfig {
|
||||
public class CXFConfig {
|
||||
|
||||
public static final String NAMESPACE_URI = "https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-65/lab-65-spring-ws-demo";
|
||||
|
||||
@@ -19,7 +19,7 @@ public class WebServicesConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Endpoint endpoint(UserService userService) {
|
||||
public Endpoint userServiceEndpoint(UserService userService) {
|
||||
Endpoint endpoint = Endpoint.create(userService);
|
||||
endpoint.publish("/user");//发布地址
|
||||
return endpoint;
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.request;
|
||||
|
||||
/**
|
||||
* 创建用户信息 Request
|
||||
*/
|
||||
public class UserCreateRequest {
|
||||
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 性别别
|
||||
*/
|
||||
private Integer gender;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public UserCreateRequest setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public UserCreateRequest setGender(Integer gender) {
|
||||
this.gender = gender;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.response;
|
||||
|
||||
/**
|
||||
* 创建用户信息 Response
|
||||
*/
|
||||
public class UserCreateResponse {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public UserCreateResponse setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.service;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.config.CXFConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.request.UserCreateRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.response.UserCreateResponse;
|
||||
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
@WebService(targetNamespace = WebServicesConfig.NAMESPACE_URI
|
||||
// ,
|
||||
// name = "userPortName"
|
||||
)
|
||||
@WebService(targetNamespace = CXFConfig.NAMESPACE_URI)
|
||||
public interface UserService {
|
||||
|
||||
UserGetResponse get(UserGetRequest request);
|
||||
|
||||
UserCreateResponse create(UserCreateRequest request);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
package cn.iocoder.springboot.lab65.userservice.service;
|
||||
|
||||
import cn.iocoder.springboot.lab65.userservice.config.WebServicesConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.config.CXFConfig;
|
||||
import cn.iocoder.springboot.lab65.userservice.request.UserCreateRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.request.UserGetRequest;
|
||||
import cn.iocoder.springboot.lab65.userservice.response.UserCreateResponse;
|
||||
import cn.iocoder.springboot.lab65.userservice.response.UserGetResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
@Service
|
||||
@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
|
||||
targetNamespace = CXFConfig.NAMESPACE_URI // WSDL 命名空间
|
||||
)
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
@@ -26,12 +25,11 @@ public class UserServiceImpl implements UserService {
|
||||
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;
|
||||
// }
|
||||
@Override
|
||||
public UserCreateResponse create(UserCreateRequest request) {
|
||||
UserCreateResponse response = new UserCreateResponse();
|
||||
response.setId((int) (System.currentTimeMillis() / 1000));
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
# CXF 配置项,对应 CxfProperties 配置类
|
||||
cxf:
|
||||
path: /ws/ # CXF CXFServlet 的匹配路径
|
||||
|
||||
server:
|
||||
port: 9090 # 设置服务器端口为 9090
|
||||
|
||||
Reference in New Issue
Block a user