当前位置:网站首页>WebService接口发布和调用
WebService接口发布和调用
2022-06-23 14:18:00 【抓手】
首先引入apache.cxf包maven坐标
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.4</version>
</dependency>发布WebService接口
接口定义
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* WebService接口
*
* @author 向振华
* @date 2022/06/22 10:20
*/
@WebService(name = "xzhWebService", targetNamespace = "http://webservice.xzh.com")
public interface XzhWebService {
@WebMethod
String xzhMethod(@WebParam(name = "xmlContent", targetNamespace = "http://webservice.xzh.com") String xmlContent);
}实现类
/**
* @author 向振华
* @date 2022/06/22 10:22
*/
public class XzhWebServiceImpl implements XzhWebService {
@Override
public String xzhMethod(String xmlContent) {
return "success → " + xmlContent;
}
}
配置
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* WebService接口配置
*
* @author 向振华
* @date 2022/06/22 10:25
*/
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean registration() {
// 注册servlet bean组件,并在路径上添加/ws(不是必须)
return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public XzhWebService webService() {
return new XzhWebServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
// 发布的服务地址
endpoint.publish("/xzh");
return endpoint;
}
}调用WebService接口
调用实际是个http请求,但是参数外层需要包装一层xml,我这里直接采用hutool工具包直接调用:
public static void main(String[] args) {
// 发送请求
SoapClient client = SoapClient.create("http://localhost:8596/bop-oms/ws/xzh")
.setMethod("xzhMethod", "http://webservice.xzh.com")
.setParam("xmlContent", "hello world!");
String res = client.send(true);
System.out.println(res);
}可以看到打印的res接口为:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:xzhMethodResponse xmlns:ns2="http://webservice.xzh.com">
<return>success → hello world!</return>
</ns2:xzhMethodResponse>
</soap:Body>
</soap:Envelope>
采用Postman调用验证也能得到相同结果:

边栏推荐
- Teach you how to build Tencent cloud server (explanation with pictures and pictures)
- ICML 2022 𞓜 context integrated transformer based auction design neural network
- 【深入理解TcaplusDB技术】TcaplusDB业务数据备份
- 2021-05-08
- golang--判断字符串是否相等
- How to merge tables when exporting excel tables with xlsx
- When did the redo log under InnoDB in mysql start to perform check point disk dropping?
- The largest IPO of Hong Kong stocks this year, with a net worth of 66billion, is the "King" sitting on the mine
- Unity realizes the function of playing Ogg format video
- Ie mode of selenium edge
猜你喜欢

k8s--部署单机版MySQL,并持久化
![[digital signal processing] linear time invariant system LTI (judge whether a system is a](/img/98/6819646ea602781101ac9994213129.jpg)
[digital signal processing] linear time invariant system LTI (judge whether a system is a "non time variant" system | case 2)

百萬獎金等你來拿,首届中國元宇宙創新應用大賽聯合創業黑馬火熱招募中!

狂奔的极兔,摔了一跤

LEGO announces price increase, speculators are more excited

AXI_Round_Robin_Arbiter 设计 - AW、W通道部分

Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!

如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup

腾讯云服务器发送邮件失败

2021-05-08
随机推荐
【深入理解TcaplusDB技术】TcaplusDB导入数据
Hot Recruitment! The second Tencent light · public welfare innovation challenge is waiting for you to participate
Networknt:: JSON schema validator source code appreciation
vim备份历史命令
Penetration test - right raising topic
小米为何深陷芯片泥潭?
ICML 2022 𞓜 context integrated transformer based auction design neural network
[datahub] LinkedIn datahub learning notes
k8s--部署单机版MySQL,并持久化
中国矿大团队,开发集成多尺度深度学习模型,用于 RNA 甲基化位点预测
从3开始,在业务系统中增加分页功能
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
基因检测,如何帮助患者对抗疾病?
Instructions for laravel8 Beanstalk
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
Golang-- judge whether the strings are equal
等保備案是什麼意思?應該去哪裏辦理備案?
Error when Oracle enters sqlplus
用OBS做直播推流简易教程
Low grain prices hurt farmers, low wages hurt farmers!