在springboot项目中搭建webservice服务端及使用客户端进行请求的介绍

  • 一、引包
  • 二、搭建webservice服务
    • (一)使用CXF搭建webservice服务
    • (二)webservice服务端
  • 三、使用postman测试
  • 四、开发客户端及使用客户端进行测试
    • (一)客户端代码
    • (二)客户端发送请求后,收到服务端的响应
    • (三)服务端的日志
  • 五、关于多个参数

一、引包

<!--    简单对象访问协议    --><dependency><groupId>soap</groupId><artifactId>soap</artifactId><version>2.3.1</version></dependency><!--    用于传输soap数据    --><dependency><groupId>org.apache.axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency><dependency><groupId>axis</groupId><artifactId>axis-jaxrpc</artifactId><version>1.4</version></dependency><dependency><groupId>axis</groupId><artifactId>axis-wsdl4j</artifactId><version>1.5.1</version></dependency><dependency><groupId>commons-discovery</groupId><artifactId>commons-discovery</artifactId><version>0.2</version></dependency><!--    起WebService服务用    --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><!--    用于搭建WebService服务的框架    --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.3.6</version><type>pom</type></dependency>

二、搭建webservice服务

(一)使用CXF搭建webservice服务

package com.bdsoft.config;import com.bdsoft.inf.service.WsServer;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;/*** WebService服务框架** @author hansc* @since 2022-03-15*/
@Configuration
public class WsCxfConfig {@AutowiredBus bus;@AutowiredWsServer wsServer;@Bean(name = "cxfServlet")public ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(), "/inf/ws/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}/*** 访问地址(localhost替换成发布的服务对应的域名):* http://localhost:8081/inf/ws/do?wsdl** @return*/@Bean(name = "endpoint")public Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(bus, wsServer);endpoint.publish("/do");return endpoint;}}

(二)webservice服务端

1、接口开发

package com.bdsoft.inf.service;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;/*** WebService服务端** @author hansc* @since 2022-03-15*/@WebService
public interface WsServer {/*** 查询** @param id* @return*/@WebMethod(operationName = "select", action = "selectAction")@WebResult(name = "result")String selectById(@WebParam(name = "id", targetNamespace = "http://service.inf.bdsoft.com/") String id);/*** 新增** @param param* @return*/@WebMethod(operationName = "insert", action = "insertAction")@WebResult(name = "result")String insert(@WebParam(name = "param", targetNamespace = "http://service.inf.bdsoft.com/") String param);/*** 修改** @param param* @return*/@WebMethod(operationName = "update", action = "updateAction")@WebResult(name = "result")String update(@WebParam(name = "param", targetNamespace = "http://service.inf.bdsoft.com/") String param);/*** 接收在某某HR系统中修改后处于工作流中的人员信息** @param id     人员主键* @param status 流程状态* @return*/@WebMethod(operationName = "updateInWorkFlow", action = "updateInWorkFlowAction")@WebResult(name = "result")String updateInWorkFlow(@WebParam(name = "id", targetNamespace = "http://service.inf.bdsoft.com/") String id,@WebParam(name = "status", targetNamespace = "http://service.inf.bdsoft.com/") String status);}

2、实现类开发

package com.bdsoft.inf.service.impl;import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bdsoft.core.constans.APPEnums;
import com.bdsoft.core.constans.error.ErrorTip;
import com.bdsoft.inf.dao.SsHrEmpBasicinfoMapper;
import com.bdsoft.inf.dao.ZjUserMapper;
import com.bdsoft.inf.entity.SsHrEmpBasicinfo;
import com.bdsoft.inf.entity.ZjUser;
import com.bdsoft.inf.service.WsServer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.jws.WebService;
import java.util.Map;/*** @author hansc* @since 2022-03-15*/@WebService(serviceName = "ZjWebService",portName = "ZjPort",name = "ZjPortType",targetNamespace = "http://service.inf.bdsoft.com/"
)
@Service
public class WsServerImpl implements WsServer {@AutowiredZjUserMapper zjUserMapper;@AutowiredSsHrEmpBasicinfoMapper basicinfoMapper;private Logger logger = LoggerFactory.getLogger(this.getClass());/*** 查询** @param id* @return*/@Overridepublic String selectById(String id) {ZjUser zjUser = zjUserMapper.selectById(id);return JSON.toJSONString(zjUser);}/*** 新增** @param param* @return*/@Override@Transactional(rollbackFor = Exception.class)public String insert(String param) {logger.debug("insert接口入参为:{}", param);ObjectMapper mapper = new ObjectMapper();try {Map map = mapper.readValue(param, Map.class);Map basicinfo = MapUtil.get(map, "HR_EMP_BASICINFO", Map.class);SsHrEmpBasicinfo bean = BeanUtil.toBean(basicinfo, SsHrEmpBasicinfo.class, new CopyOptions().ignoreCase().ignoreNullValue());logger.debug("转换后的bean对象为:{}", bean);int insert = basicinfoMapper.insert(bean);return insert == 1 ?getJsonResult(APPEnums.OK).toJSONString() :getJsonResult(APPEnums.PARAM_ERROR_TIP).toJSONString();} catch (JsonProcessingException e) {e.printStackTrace();}return getJsonResult(APPEnums.PARAM_ERROR_TIP).toJSONString();}/*** 修改** @param param* @return*/@Override@Transactional(rollbackFor = Exception.class)public String update(String param) {logger.debug("update接口入参:{}", param);return StrUtil.isBlank(param) ?getJsonResult(APPEnums.PARAM_ERROR_TIP).toJSONString() :getJsonResult(APPEnums.OK).toJSONString();}/*** 接收在某某HR系统中修改后处于工作流中的人员信息** @param id     人员主键* @param status 流程状态* @return*/@Override@Transactional(rollbackFor = Exception.class)public String updateInWorkFlow(String id, String status) {logger.debug("updateInWorkFlow接口入参id={}", id);logger.debug("updateInWorkFlow接口入参status={}", status);return StrUtil.isBlank(id) ?getJsonResult(APPEnums.PARAM_ERROR_TIP).toJSONString() :getJsonResult(APPEnums.OK).toJSONString();}/*** 获取返回值的json对象** @param errorTip* @return*/private JSONObject getJsonResult(ErrorTip errorTip) {JSONObject jsonObject = new JSONObject();jsonObject.put("code", errorTip.getCode());jsonObject.put("message", errorTip.getMessage());return jsonObject;}}

3、常量类

package com.bdsoft.inf.util;/*** @author hansc* @since 2022-03-17*/
public class WsConstants {public static String LOCAL_URL = "http://localhost:8081/inf/ws/do?wsdl";public static String TARGET_NAMESPACE = "http://service.inf.bdsoft.com/";}

启动服务后在浏览器中访问WSDL文档

三、使用postman测试


请求xml:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><select xmlns="http://service.inf.bdsoft.com/"><id>zt11111</id></select></soap:Body>
</soap:Envelope>

响应xml:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:updateInWorkFlowResponse xmlns:ns2="http://service.inf.bdsoft.com/"><result>{"code":200,"message":"操作成功!"}</result></ns2:updateInWorkFlowResponse></soap:Body>
</soap:Envelope>

请求路径为:http://localhost:8081/inf/ws/do
请求方式为:POST
数据类型为:Content-Type = text/xml; charset=utf-8
数据格式为:raw,XML(text/xml)

多个参数的请求方式

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><updateInWorkFlow xmlns="http://service.inf.bdsoft.com/"><id>人员ID</id><status>工作流状态</status></updateInWorkFlow></soap:Body>
</soap:Envelope>

四、开发客户端及使用客户端进行测试

(一)客户端代码

package com.bdsoft.inf.controller;import com.bdsoft.inf.util.WsConstants;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;/*** WebService客户端** @author hansc* @since 2022-03-14*/
public class WsClient {private static Logger logger = LoggerFactory.getLogger(WsClient.class);public static void select() {try {String url = WsConstants.LOCAL_URL;String targetNamespace = WsConstants.TARGET_NAMESPACE;String operationName = "select";Service service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(url);call.setOperationName(new QName(targetNamespace, operationName));call.addParameter(new QName(targetNamespace, "id"),XMLType.XSD_STRING,ParameterMode.IN);call.setReturnType(XMLType.XSD_STRING);call.setUseSOAPAction(true);call.setSOAPActionURI("selectAction");String result = (String) call.invoke(new Object[]{"zt11111"});System.out.println("WebService返回值" + result);} catch (Exception e) {e.printStackTrace();}}public static void update() {try {String url = WsConstants.LOCAL_URL;String targetNamespace = WsConstants.TARGET_NAMESPACE;String operationName = "updateInWorkFlow";Service service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(url);call.setOperationName(new QName(targetNamespace, operationName));call.addParameter(new QName(targetNamespace, "id"), XMLType.XSD_STRING, ParameterMode.IN);call.addParameter(new QName(targetNamespace, "status"), XMLType.XSD_STRING, ParameterMode.IN);call.setEncodingStyle("UTF-8");call.setReturnType(XMLType.XSD_STRING);call.setUseSOAPAction(true);call.setSOAPActionURI("updateInWorkFlowAction");Object invoke = call.invoke(new Object[]{"7758258", "修改中"});logger.debug("这是客户端的请求---->");logger.debug("这是服务端的响应:{}", invoke);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {update();}}

(二)客户端发送请求后,收到服务端的响应

(三)服务端的日志

五、关于多个参数

服务端的方法中,参数前的@WebParam注解中,targetNamespace属性需要显式命名,就像我写的updateInWorkFlow方法那样。
请求时,在postman中,用多个标签来区分形参名:

在客户端请求时,通过call.addParameter()方法,添加多个参数,注意在QName中需要指定命名空间,与@WebParam注解中的targetNamespace相同

关于webservice服务在springboot项目中的开发的介绍相关推荐

  1. 亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像)

    亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像) 大噶好,我们继续延续上一章,学习如何使用OSS存放用户头像代码示例; 在application.propert ...

  2. Spring-Boot:写出来的网站访问不到静态资源?怎样通过url访问SpringBoot项目中的静态资源?localhost:8989/favicon.ico访问不了工程中的图标资源?

    Spring-Boot:Spring-Boot写出来的网站访问不到静态资源?怎样通过url访问SpringBoot项目中的静态资源?localhost:8989/favicon.ico访问不了工程中的 ...

  3. 使用IDEA在SpringBoot项目中连接数据库

    使用IDEA在SpringBoot项目中连接数据库 文章目录 使用IDEA在SpringBoot项目中连接数据库 前言 连接数据库 正常操作数据库 移除数据库连接 前言 每次我在 IDEA 中用 Sp ...

  4. IDEA springboot项目中properties配置文件 {针对将对应GBK改为UTF-8并勾选转为ASCII后仍无效情况} 运行时中文乱码解决

    springboot项目中properties配置文件中,运行时中文乱码情况 file encoding里边进行设置,设为utf-8并勾选转为ascii,分别在setting.setting for ...

  5. springboot项目中pom.xml文件的颜色变成灰色,图标变成蜘蛛图形

    问题 今天springboot项目中pom.xml文件的图标突然变成蜘蛛图案,pom.xml的内容大量报红,但项目任然可以正常运行 解决方法 点击idea右侧的 AntBuild,找到pom.xml, ...

  6. springboot项目中使用shiro 自定义过滤器和token的方式___shiro使用token登录流程

    springboot项目中使用shiro 自定义过滤器和token的方式 实现步骤主要是以下几步: 1. 在项目中导入maven依赖 <dependency><groupId> ...

  7. springboot项目中mybatis实现数据的基本查询

    SpringBoot项目中mybatis实现数据的基本查询 本章内容概述: mapper 查询 xml 文件基本使用 通过 mybatis 实现一条数据的查询 1 用户数据表 2 用户信息对应的实体类 ...

  8. SpringBoot项目中遇到的BUG

    1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...

  9. SpringBoot项目中图片的引用

    问题描述:在SpringBoot项目中需要在CSS样式文件中引入图片给某个元素设置样式 解决办法: body{background-image: url("../image/580.jpg& ...

最新文章

  1. 从李小龙的一句话看程序员是否应该多学几种编程语言
  2. Unity导出APk出错解决方法二
  3. 【XML DOM】解析XML Dom
  4. [zz]GMM-HMM语音识别模型 原理篇
  5. linux c的连接库和怎么同时编译多个源程序
  6. java生成四则运算表达式_生成四则运算(java实现)
  7. 实验五 类和对象-3
  8. element-ui的基本使用(一)
  9. linux搭建tht框架,教程 中标麒麟linux硬盘安装图解
  10. RabbitMQ之Channel
  11. 微分中值定理之柯西中值定理
  12. 一段集大成的thymeleaf代码
  13. video视频快进拖动限制
  14. Grafana 导出所有dashboard
  15. python抓取视频违法吗,科学网—【python爬虫】抓取B站视频相关信息(一) - 管金昱的博文...
  16. maven_防止在多模块Maven中找到“未找到插件”
  17. 去百度,还是去创新工场
  18. anaconda创建虚拟环境报错
  19. 远程代答系统在使用中常见问题解答
  20. 历年Infoq架构师月刊收集

热门文章

  1. 洛谷P2791 幼儿园篮球题
  2. 基于51单片机的DS12C887电子钟万年历带农历温度
  3. H3C恢复console登录密码
  4. 如何根据ACPI规范来获取I/O APIC控制寄存器的地址
  5. 强基计划 数学相关书籍 推荐
  6. MT【18】幂平均不等式的证明
  7. 【kafka】解决kafka-tool连接上kafka,brokers和topics不显示问题
  8. Qt获取wifi列表,连接wifi后获取IP地址
  9. java实现手写签名_手写签字,保存笔迹到图片
  10. 使用Python将MNIST数据集手写数字转化为图片