springboot 整合webservice 相关说明
1.环境依赖 jdk8, springboot 2.3.12.release,cxf版本需要根据springboot版本修改,方法:查看springboot版本的发布日期,然后根据日期找相近的两个版本
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.12.RELEASE</version>
</dependency> <dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.5</version>
</dependency>
2. WebserviceConfig.java

1 package com.example.demo.demos.nacosconfig;
2
3 import javax.xml.ws.Endpoint;
4
5 import org.apache.cxf.bus.spring.SpringBus;
6 import org.apache.cxf.jaxws.EndpointImpl;
7 import org.apache.cxf.transport.servlet.CXFServlet;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.web.servlet.ServletRegistrationBean;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12
13 import com.example.demo.demos.webservice.IUserWebService;
14
15 @Configuration
16 public class WebServiceConfig {
17
18 /************* 要发布的webservice,需要有对应的实现类,建议参照下文中的命名规范 ****************/
19 @Autowired
20 private IUserWebService webService;
21
22 @SuppressWarnings({ "rawtypes", "unchecked" })
23 @Bean
24 public ServletRegistrationBean cxfServlet() {
25 return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
26 }
27
28 @Bean
29 public SpringBus cxf() {
30 return new SpringBus();
31 }
32
33 @Bean
34 public Endpoint endpoint() {
35 EndpointImpl endpoint = new EndpointImpl(cxf(), webService);
36 endpoint.publish("/api");
37 return endpoint;
38 }
39 }
3. 接口示例和规范

1 package com.example.demo.demos.webservice;
2
3 import javax.jws.WebMethod;
4 import javax.jws.WebService;
5
6 /**
7 * name决定了生成的相关调用代码是否简单明了且符合上下文语境<br/>
8 * targetNamespace 无所谓,和实现类一致即可
9 * @author Administrator
10 *
11 */
12 @WebService(name = "userService", targetNamespace = "https://www.demo.tech")
13 public interface IUserWebService {
14
15 @WebMethod
16 public String sayHello(String name);
17 }
4.实现类

1 package com.example.demo.demos.webservice.impl;
2
3 import javax.jws.WebService;
4
5 import org.springframework.stereotype.Component;
6
7 import com.example.demo.demos.webservice.IUserWebService;
8
9 /**
10 *
11 * class 类名如下,去掉接口中的首字母'I'和结尾单词'Service'<br/>
12 * 生成的调用代码语境就比较通顺
13 *
14 * @author Administrator
15 *
16 */
17 @Component
18 @WebService(targetNamespace = "https://www.demo.tech", endpointInterface = "com.example.demo.demos.webservice.IUserWebService")
19 public class UserWeb implements IUserWebService {
20
21 public String sayHello(String name) {
22 return String.format("hello,%s", name);
23 }
24 }
5. 打开要生成的文件夹,即生成的代码要保存到哪里就打开哪个文件夹,然后运行命令 wsimport -encoding UTF-8 -s . http://127.0.0.1:8080/ws/api?wsdl 自动生成java相关代码
6. 测试代码就会变得语境通顺

1 package com.example.demo;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import javax.xml.namespace.QName;
7 import javax.xml.ws.Service;
8
9 import https.www_zqxx.UserService;
10
11 public class WebServiceTest {
12 public static void main(String[] args) throws MalformedURLException {
13 System.setProperty("javax.xml.bind.JAXBContext", "com.sun.xml.internal.bind.v2.ContextFactory");
14 // 创建WSDL的URL
15 URL url = new URL("http://localhost:8080/ws/api?wsdl");
16 // 指定命名空间和服务名称
17 QName qName = new QName("https://www.demo.tech", "UserWebService");
18 Service service = Service.create(url, qName);
19 // 通过getPort方法返回指定接口
20 UserService userService = service.getPort(UserService.class);
21 String res = userService.sayHello("小明");
22 System.out.println(res);
23 }
24 }
7. 统一封装结果类

package com.zqxxjs.v1.common; import java.util.List; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "ResponseEntity")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "code", "msg", "result" })
public class ResponseEntity<T> { private Integer code;
private String msg; private Message<T> result; public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public Message<T> getResult() {
return result;
} public void setResult(Message<T> result) {
this.result = result;
} public static <E> ResponseEntity<E> success(List<E> data, Long count) {
ResponseEntity<E> entity = new ResponseEntity<>();
entity.setCode(Constant.SUCCESS_CODE);
entity.setMsg(Constant.SUCCESS_MSG);
Message<E> resultTmp = new Message<>();
resultTmp.setCount(count);
resultTmp.setData(data);
entity.setResult(resultTmp);
return entity;
} }
8. 配合mbg插件,在生成的实体类上边要加的相关注解,注解生成工具:

package com.zqxxjs.order.common; import java.io.File;
import java.lang.reflect.Field;
import java.util.Collection; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; public class XmlUtil {
public static void main(String[] args) throws Exception {
String packageName="com.zqxxjs.order.entity";
String path = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/")).getPath();
Collection<File> entityClassFile = FileUtils.listFiles(new File(path), new String[] { "class" }, false);
for (File file : entityClassFile) {
if (file.getName().contains("Example")) {
continue;
}
Class<?> c = Class.forName(packageName+"." + FilenameUtils.getBaseName(file.getName()));
Field[] fds = c.getDeclaredFields();
String s = ("@XmlType(propOrder = { ");
String[] tmp = new String[fds.length - 1];
for (int i = 0; i < tmp.length; i++) {
Field f = fds[i];
if (f.getName().equalsIgnoreCase("serialVersionUID")) {
continue;
} tmp[i] = String.format("\"%s\"", f.getName());
} System.out.println("\n\n@XmlRootElement(name = \""+c.getSimpleName()+"\")");
System.out.println("@XmlAccessorType(XmlAccessType.FIELD)");
s = s.concat(String.join(",", tmp)).concat("})");
System.out.println(s);
System.out.println("-----------------------------------------------------------------");
} }
}
9. 加好后的示例代码

package com.zqxxjs.v1.entity; import java.io.Serializable;
import java.math.BigDecimal; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; /**
*
* This class was generated by MyBatis Generator. This class corresponds to the
* database table finance_account_info
*/ @XmlRootElement(name = "FinanceAccountInfo")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "id", "serialNumber", "projectNumber", "projectName", "director", "amountOfMoney", "createDate",
"financeNumber", "remark" })
public class FinanceAccountInfo implements Serializable {
/**
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.id
*
* @mbg.generated
*/
private Integer id; /**
* Database Column Remarks: 流水号
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.serial_number
*
* @mbg.generated
*/
private String serialNumber; /**
* Database Column Remarks: 项目编号
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.project_number
*
* @mbg.generated
*/
private String projectNumber; /**
* Database Column Remarks: 项目名称
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.project_name
*
* @mbg.generated
*/
private String projectName; /**
* Database Column Remarks: 负责人
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.director
*
* @mbg.generated
*/
private String director; /**
* Database Column Remarks: 金额
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.amount_of_money
*
* @mbg.generated
*/
private BigDecimal amountOfMoney; /**
* Database Column Remarks: 往来日期
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.create_date
*
* @mbg.generated
*/
private String createDate; /**
* Database Column Remarks: 财务编号
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.finance_number
*
* @mbg.generated
*/
private String financeNumber; /**
* Database Column Remarks: 备注
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column finance_account_info.remark
*
* @mbg.generated
*/
private String remark; /**
* This field was generated by MyBatis Generator. This field corresponds to the
* database table finance_account_info
*
* @mbg.generated
*/
private static final long serialVersionUID = 1L; /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.id
*
* @return the value of finance_account_info.id
*
* @mbg.generated
*/
public Integer getId() {
return id;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.id
*
* @param id the value for finance_account_info.id
*
* @mbg.generated
*/
public void setId(Integer id) {
this.id = id;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.serial_number
*
* @return the value of finance_account_info.serial_number
*
* @mbg.generated
*/
public String getSerialNumber() {
return serialNumber;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.serial_number
*
* @param serialNumber the value for finance_account_info.serial_number
*
* @mbg.generated
*/
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.project_number
*
* @return the value of finance_account_info.project_number
*
* @mbg.generated
*/
public String getProjectNumber() {
return projectNumber;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.project_number
*
* @param projectNumber the value for finance_account_info.project_number
*
* @mbg.generated
*/
public void setProjectNumber(String projectNumber) {
this.projectNumber = projectNumber;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.project_name
*
* @return the value of finance_account_info.project_name
*
* @mbg.generated
*/
public String getProjectName() {
return projectName;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.project_name
*
* @param projectName the value for finance_account_info.project_name
*
* @mbg.generated
*/
public void setProjectName(String projectName) {
this.projectName = projectName;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.director
*
* @return the value of finance_account_info.director
*
* @mbg.generated
*/
public String getDirector() {
return director;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.director
*
* @param director the value for finance_account_info.director
*
* @mbg.generated
*/
public void setDirector(String director) {
this.director = director;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.amount_of_money
*
* @return the value of finance_account_info.amount_of_money
*
* @mbg.generated
*/
public BigDecimal getAmountOfMoney() {
return amountOfMoney;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.amount_of_money
*
* @param amountOfMoney the value for finance_account_info.amount_of_money
*
* @mbg.generated
*/
public void setAmountOfMoney(BigDecimal amountOfMoney) {
this.amountOfMoney = amountOfMoney;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.create_date
*
* @return the value of finance_account_info.create_date
*
* @mbg.generated
*/
public String getCreateDate() {
return createDate;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.create_date
*
* @param createDate the value for finance_account_info.create_date
*
* @mbg.generated
*/
public void setCreateDate(String createDate) {
this.createDate = createDate;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.finance_number
*
* @return the value of finance_account_info.finance_number
*
* @mbg.generated
*/
public String getFinanceNumber() {
return financeNumber;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.finance_number
*
* @param financeNumber the value for finance_account_info.finance_number
*
* @mbg.generated
*/
public void setFinanceNumber(String financeNumber) {
this.financeNumber = financeNumber;
} /**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column finance_account_info.remark
*
* @return the value of finance_account_info.remark
*
* @mbg.generated
*/
public String getRemark() {
return remark;
} /**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column finance_account_info.remark
*
* @param remark the value for finance_account_info.remark
*
* @mbg.generated
*/
public void setRemark(String remark) {
this.remark = remark;
}
}
springboot 整合webservice 相关说明的更多相关文章
- Springboot整合webservice
Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...
- springboot整合WebService简单版
一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...
- springboot整合webservice采用CXF技术
转载自:https://blog.csdn.net/qq_31451081/article/details/80783220 强推:https://blog.csdn.net/chjskarl/art ...
- 【Springboot】实例讲解Springboot整合OpenTracing分布式链路追踪系统(Jaeger和Zipkin)
1 分布式追踪系统 随着大量公司把单体应用重构为微服务,对于运维人员的责任就更加重大了.架构更复杂.应用更多,要从中快速诊断出问题.找到性能瓶颈,并不是一件容易的事.因此,也随着诞生了一系列面向Dev ...
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- 很详细的SpringBoot整合UEditor教程
很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...
- SpringBoot整合Apache-CXF实践
一.Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS . ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
随机推荐
- obs 屏幕录像 1. 屏幕放大缩小 2. 圆形摄像头
obs 屏幕录像 1. 屏幕放大缩小 2. 圆形摄像头 屏幕放大缩小 windows 放大镜 屏幕放大快捷键 win + '+' 是放大屏幕 win + '-' 是缩小屏幕 因为obs不支持缩放功能, ...
- 数组动态表单验证,添加数组,逆序添加,表单验证会错位,发现是key的默认index问题,还有验证trigger问题,添加数据会爆红
数组动态表单验证,添加数组,逆序添加,表单验证会错位,发现是key的默认index问题,还有验证trigger问题,添加数据会爆红 解决方案: trigger: 'blur,change' 换 tri ...
- python中把列表中的字符串转成整型的几种方法
python中在不同类型数据转换方面是有标准库的,使用非常方便.但是在开发中,经常在list中字符转成整形的数据方便遇到问题.这里就记录一下常用的几种方法. 方法一:最基本的方法:通过轮训整个list ...
- Android开发过程中的坑及解决方法收录(7)
1.无法找到自定义View的实例对象 问题描述: 由于经常要使用到两个TextView的布局,所以,我打算将其封装成一个自定义View,封装成功,界面能够成功显示了,但是,想给它设置点击监听器的时候就 ...
- linux系统运行时参数命令、网络、磁盘参数和日志监控
重点内容 linux基础命令和工具 CPU性能监控 内存性能监控 文件IO性能监控 网络IO监控 1 linux基础命令和工具 1.1 Grep搜索字符 grep命令用于在文件中执行关键字搜索,并显示 ...
- linux权限、特殊权限、ACL控制
Linux基本权限 1.权限基本概述 1.什么是权限? 我们可以把它理解为操作系统对用户能够执行的功能所设立的限制,主要用于约束用户能对系统所做的操作,以及内容访问的范围,或者说,权限是指某个特定的用 ...
- python面向对象(绑定方法与非绑定方法)
# 一.绑定方法:特殊之处在于将调用者本身当做第一个参数自动传入 # 1)绑定给对象的方法:调用者是对象,自动传入的是对象 # 2)绑定给类的方法:调用者是类,自动传入的是类 # 类方法的应用 # 需 ...
- 《Go程序设计语言》学习笔记之map
<Go程序设计语言>学习笔记之map 一. 环境 Centos8.5, go1.17.5 linux/amd64 二. 概念 1) 散列表,是一个拥有键值对元素的无序集合.在这个集合中,键 ...
- hdfs disk balancer 磁盘均衡器
目录 1.背景 2.hdfs balancer和 hdfs disk balancer有何不同? 3.操作 3.1 生成计划 3.2 执行计划 3.3 查询计划 3.4 取消计划 4.和disk ba ...
- 使用maven命令安装Oracle的jar包到本地仓库
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.4 -Dpackaging=jar ...