当前位置:网站首页>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调用验证也能得到相同结果:

边栏推荐
- 2021-06-03
- Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!
- 2021-05-08
- WPF (c) new open source control library: newbeecoder UI waiting animation
- Golang-- judge whether the strings are equal
- 操作系统底层知识总结(面试)
- When did the redo log under InnoDB in mysql start to perform check point disk dropping?
- 微信小程序引导用户添加小程序动画页
- Use of pyqt5 tool box
- Teach you how to build Tencent cloud server (explanation with pictures and pictures)
猜你喜欢

An idea plug-in for automatically generating unit tests

中国矿大团队,开发集成多尺度深度学习模型,用于 RNA 甲基化位点预测

Error when Oracle enters sqlplus

加快 yarn install 的三个简单技巧
![[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?](/img/84/4fa07c54786aaf423ca333ecde62f4.png)
[Level 2 warranty] which brand of Fortress machine is good for Level 2 warranty?

【深入理解TcaplusDB技术】TcaplusDB导入数据

Technology sharing | do you understand the requirements of the tested project?

建議自查!MySQL驅動Bug引發的事務不回滾問題,也許你正面臨該風險!

2021-05-08

操作系统底层知识总结(面试)
随机推荐
The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
NFNet:NF-ResNet的延伸,不用BN的4096超大batch size训练 | 21年论文
MySQL 创建和管理表
ICML 2022 𞓜 context integrated transformer based auction design neural network
JSR303数据校验
Effect evaluation of regression model under credit product quota pricing scenario
乐高宣布涨价,炒家更嗨皮了
How to ensure long-term stable operation of EDI system
2022 ICT market in China continues to rise and enterprise digital infrastructure is imperative
建議自查!MySQL驅動Bug引發的事務不回滾問題,也許你正面臨該風險!
杀入美团、饿了么腹地,京东外卖劲儿有多大?
What do you mean by waiting for insurance records? Where should I go for filing?
加快 yarn install 的三个简单技巧
系统设计与分析-技术报告-定时清理验证码的一种解决方案
Networknt:: JSON schema validator source code appreciation
Soaring 2000+? The average salary of software testing in 2021 has come out, and I can't sit still
The principle of redis cache consistency deep analysis
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
去 OPPO 面试, 被问麻了。。。
腾讯云服务器发送邮件失败