SpringBoot整合WebService

简介

WebService就是一种跨编程语言和跨操作系统平台的远程调用技术

此处就不赘述WebService相关概念和原理了,可以参考:https://blog.csdn.net/c99463904/article/details/76018436

代码示例

此处共分两个端,客户端和服务端,一个负责调用接口,一个负责创建接口并实现

首先创建一个Maven父项目,在pom.xml文件中引入SpringBoot坐标

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Server搭建

在父项目中创建服务端(Server)子项目,

pom.xml文件,引入以下坐标

<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<!-- 不引入会报错 报接口未实现 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

启动类(引导类)

@SpringBootApplication
public class ServerApplication { public static void main(String[] args) {
SpringApplication.run(ServerApplication.class,args);
} }

User.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User { private String name; private Integer age; }

UserService接口

@WebService(targetNamespace = "wsdl.aerfazhe.com",name = "userPortType")
public interface UserService { @WebMethod
User getUserName(@WebParam(name = "name") String name); }

UserService实现类

@WebService(
targetNamespace = "wsdl.aerfazhe.com", //命名空间 指定wsdl说明书在Client端所存放的包路径为com.aerfazhe.wsdl包下
name = "userPortType",
serviceName = "userService", //服务Name名称
portName = "userPortName",
endpointInterface = "com.aerfazhe.service.UserService" // 指定webService的接口类,此类也需要接入@WebService注解
)
public class UserServiceImpl implements UserService { @Override
public User getUserName(String name) {
User user = new User(name, 28);
return user;
} }

配置类

@Configuration
public class CxfWebServiceConfig { @Bean("cxfServletRegistration")
public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
return new ServletRegistrationBean<>(new CXFServlet(),"/ws/*");
} @Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
} @Bean
public UserService userService() {
return new UserServiceImpl();
} @Bean
public Endpoint endpoint(UserService userService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
endpoint.publish("/user");
return endpoint;
} }

启动后访问wsdl说明书

http:localhost:8080/ws/user?wsdl

Client搭建

pom.xml

<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<!-- 不引入会报错 报接口未实现 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>src/main/resources/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:8080/ws/user?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

配置类

@Configuration
public class CxfClientConfig { private final static String SERVICE_ADDRESS = "http://localhost:8080/ws/user"; @Bean("cxfProxy")
public UserPortType createUserPortTypeProxy() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress(SERVICE_ADDRESS);
jaxWsProxyFactoryBean.setServiceClass(UserPortType.class);
return (UserPortType) jaxWsProxyFactoryBean.create();
}
}

编译转换

将XML格式的wsdl说明书转化为Java对象格式

在Client客户端项目根路径下调出控制台,如下

使用maven命令进行转换,生成的Java代码在Client客户端下的src/main/resources/cxf目录下

mvn generate-sources

查看src/main/resources/cxf下的文件,如下

创建com/aerfazhe/wsdl包路径,将这些JavaObject剪切到该包路径下,如下

Controller

@RestController
public class UserController { @Resource(name = "cxfProxy")
private UserPortType userPortType; @GetMapping("/getUserName")
public User getUserName(String name) {
User user = userPortType.getUserName(name);
return user;
} }

启动类

@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class,args);
}
}

application.yml

server:
port: 8081

在当前项目根目录下,输入以下命令,将xml类型的wsdl说明书转化为Java对象

mvn generate-sources

启动后访问:http://localhost:8081/getUserName?name=张三

此时就调用成功喽

SpringBoot整合WebService(实用版)的更多相关文章

  1. Springboot整合webservice

    Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...

  2. springboot整合WebService简单版

    一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...

  3. springboot整合webservice采用CXF技术

    转载自:https://blog.csdn.net/qq_31451081/article/details/80783220 强推:https://blog.csdn.net/chjskarl/art ...

  4. 很详细的SpringBoot整合UEditor教程

    很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529    版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...

  5. springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean

    在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本  3.1.7 后更新为 ...

  6. idea使用springboot的webservice基于cxf

    SpringBoot整合CXF实例: 服务端构建 <dependency> <groupId>org.apache.cxf</groupId> <artifa ...

  7. SpringBoot整合Apache-CXF实践

    一.Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS . ...

  8. Spring整合CXF步骤,Spring实现webService,spring整合WebService

    Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...

  9. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  10. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

随机推荐

  1. 指针和引用(pointer and reference),传值和传址

    pass by adress pass by reference和pass by pointer的共同点都在于传址,都是对于对象的地址的复制,而不会对对象进行产生副本的操作. pass by refe ...

  2. docker上面部署nginx-waf 防火墙“modsecurity”,使用CRS规则,搭建WEB应用防火墙

    web防火墙(waf)免费开源的比较少,并且真正可以商用的WAF少之又少,modsecurity 是开源防火墙鼻祖并且有正规公司在维护着,目前是https://www.trustwave.com在维护 ...

  3. Linux shell和环境变量

    环境变量 存储有关shell会话和工作环境信息:允许在内存中存储数据. 注意什么时候要用$ 什么时候不用$:用到变量,需要$;操作变量,不需要$.printenv除外 分为两类: 全局变量:对shel ...

  4. 理解Java程序的执行

    main 方法 public class Solution { public static void main(String[] args) { Person person = new Person( ...

  5. 3. 面向对象编程(OOP):

    面向对象编程的本质就是:以类的方式组织代码.以对象的组织(封装)数据 抽象:就是把不同的物品的共同点剥离出来,构成一个类.如每个人都有2条腿,我们可以把2条腿剥离出来 构成一个类 类与对象的关系 类: ...

  6. Jmeter-测试报告模板分享

    1.jmeter-results-detail-report_21 <?xml version="1.0"?> <!-- ~ Licensed to the Ap ...

  7. 2022-09-01:字符串的 波动 定义为子字符串中出现次数 最多 的字符次数与出现次数 最少 的字符次数之差。 给你一个字符串 s ,它只包含小写英文字母。请你返回 s 里所有 子字符串的 最大波

    2022-09-01:字符串的 波动 定义为子字符串中出现次数 最多 的字符次数与出现次数 最少 的字符次数之差. 给你一个字符串 s ,它只包含小写英文字母.请你返回 s 里所有 子字符串的 最大波 ...

  8. Vue 新建项目+基本语法

    新建项目: 导入依赖:    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"> ...

  9. HTML渲染机制

    一直写页面但是很少对一些较深的运行机制的了解,这次趁休假查了一些相关的资料加上个人理解,记录一下关于html渲染的整个过程,也加深一下自己对html渲染的理解 一.先借一张图来看看html的整个加载过 ...

  10. js原型和原型链(用代码理解代码)

    众所周知js原型及原型链是很多开发者的一个疼点(我也不例外),我也曾多次被问起,也问过不少其他人,如果在自己没有真正的去实践和理解过:那么突然之间要去用最简单的话语进行概述还真不是一件容易的事情: 其 ...