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

边栏推荐
- Introduction to helm basics helm introduction and installation
- As a software testing practitioner, do you understand your development direction?
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
- Distributed database uses logical volume to manage storage expansion
- Sqlserver2008r2 failed to install DTS component
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- Un million de bonus vous attend, le premier concours d'innovation et d'application de la Chine Yuan cosmique Joint Venture Black Horse Hot Recruitment!
- Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!
- TS encapsulation request
- 从3开始,在业务系统中增加分页功能
猜你喜欢

阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题

3 interview salary negotiation skills, easily win 2K higher than expected salary to apply for a job

【深入理解TcaplusDB技術】TcaplusDB構造數據

用OBS做直播推流简易教程

The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine

操作系统底层知识总结(面试)

An idea plug-in for automatically generating unit tests

建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!

Binding events of wechat applet in wx:for

Babbitt | metauniverse daily must read: meta, Microsoft and other technology giants set up the metauniverse Standards Forum. Huawei and Alibaba joined. NVIDIA executives said that they welcomed partic
随机推荐
When did the redo log under InnoDB in mysql start to perform check point disk dropping?
2021-06-07
WPF (c) open source control library: newbeecoder Nbexpander control of UI
How to make food nutrition label
[datahub] LinkedIn datahub learning notes
杀入美团、饿了么腹地,京东外卖劲儿有多大?
Problems during MySQL uninstallation
Thinking and Practice on Quality Standardization (suitable for product, development, testing and management post learning)
期货怎么开户安全吗,期货手续费哪家期货公司比较低,适合散户开户?
一款自动生成单元测试的 IDEA 插件
百萬獎金等你來拿,首届中國元宇宙創新應用大賽聯合創業黑馬火熱招募中!
加快 yarn install 的三个简单技巧
Do you know which position in the IT industry has the most girls?
等保備案是什麼意思?應該去哪裏辦理備案?
Effect evaluation of regression model under credit product quota pricing scenario
2022 college entrance examination quarterly essay winners announced
【DataHub】LinkedIn DataHub学习笔记
The largest IPO of Hong Kong stocks this year, with a net worth of 66billion, is the "King" sitting on the mine
JSR303数据校验
ts封装请求