一  准备工作

(一)开发环境

demo以springboot为基础框架,使用到了httpclient、hutool等依赖,详情如下:

        springboot版本:

org.springframework.boot
spring-boot-starter-parent
2.7.8

cxf与httpclient 、hutool依赖:

    <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
    <dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.20</version>
</dependency>

二 发布接口

首先创建一个测试接口,@WebService声明这是一个webservice接口,name为接口名称,targetNamespace 很重要,表明webservice接口的命名空间。@WebMethod()声明这是一个接口下的函数方法,@WebParam声明函数需要的参数。

@WebService(name = "UnifySearchService", targetNamespace = "http://com.test.webservice/service")

public interface UnifySearchService {

@WebMethod()
String testService(@WebParam(name = "parameter") String parameter);

}

创建该接口的实现类就可以在函数种编写业务处理代码:

@Override
public String testService(String parameter) {
// code return "parameter:" + parameter;
}

创建发布webservice接口的配置文件:

@Configuration

public class CxfWebServiceConfig {

@Resource
private UnifySearchService unifySearchService; @Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
} /**
* 访问地址 http://127.0.0.1:8085/ws/service?wsdl
*/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), unifySearchService);
endpoint.publish("/service");
return endpoint;
}

}

在applicaiton.properties中,添加以下配置:

创建完毕后运行项目,访问http://127.0.0.1:8085/ws/service?wsdl,可见以下内容:

到此,webservice接口就发布成功了。

三  接口调用

(一)httpclient调用

webservice接口调用在此展示两种最简单的方式,先说最简单的调用方法,httpclient方法调用,需要使用soapUI工具生成xml请求体:

再创建httpclient请求,将上面的xml请求体作为请求参数soapXml,发送POST请求:

public static String doPostSoap(String postUrl, String soapAction, String soapXml) throws IOException {
String retStr = "";
CloseableHttpClient httpClient = CustomerHttpClient4.getHttpClient();
HttpPost httpPost = new HttpPost(postUrl);
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpPost.setEntity(data);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity httpEntity = response.getEntity();
String entity = EntityUtils.toString(httpEntity, "UTF-8");
if (response.getStatusLine().getStatusCode() == 200) {
retStr = entity;
}
}
return retStr;
} public static void main(String[] args) {
String result;
String xmlParam = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://com.wp.webservice/service\"><soapenv:Header/><soapenv:Body><ser:testService><parameter>HTTP client请求</parameter></ser:testService>/soapenv:Body></soapenv:Envelope>";
try {
result = doPostSoap("http://localhost:8085/ws/service?wsdl", "", xmlParam);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(result);
}

调用结果:

(二)hutool工具调用

基于hutool提供的SoapClient工具创建webservice请求调用:

public static String request(String url, String method, String targetNamespace, Map<String, Object> param) {
SoapClient client = SoapClient.create(url).setMethod(method, targetNamespace).setParams(param, false);
// 打印组装xml请求体
Console.log(client.getMsgStr(true)); String result = client.send();
return result;
} public static void main(String[] args) {
Map<String, Object> param = new HashMap<>();
param.put("parameter", "hutool请求webservice接口");
String result = request("http://localhost:8085/ws/service?wsdl",
"ser:testService",
"http://com.wp.webservice/service",
param
);
System.out.println(result);
}

调用结果:

调用webservice接口除以上两种简便的方式外,还可使用cxf提供的工具类进行调用,具体的调用方法后续有时间再贴出来。

此外我在工作中遇到的是带有head认证的webservice接口,刚开始使用cxf框架,在拦截器中进行头部参数认证,但实际效果并不好,因此查找了以上两种方法,第一种可以适配所有情况,第二种需要进一步完善代码才可以,在后续的文章中会贴出代码,包括如何使用hutool调用带有head认证的webservice接口与cxf动态调用webservice接口,敬请期待。

Java调用与发布Webservice接口(一)的更多相关文章

  1. java调用.net的webservice接口

    要调用webservice,首先得有接口,用已经写好的接口地址在myEclipse的目标project中,右键->new web service client-> 选择JAX-WS方式,点 ...

  2. java 调用wsdl的webservice接口 两种调用方式

    关于wsdl接口对于我来说是比较头疼的 基本没搞过.一脸懵 就在网上搜 看着写的都很好到我这就不好使了,非常蓝瘦.谨以此随笔纪念我这半个月踩过的坑... 背景:短短两周除了普通开发外我就接到了两个we ...

  3. java调用第三方的webservice应用实例

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...

  4. java调用第三方的webservice应用实例【转载】

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...

  5. Linux虚拟机:发布WebService接口出现异常,无法访问接口

    Linux虚拟机:发布WebService接口出现异常,无法访问接口 今天在部署WebService工程的时候遇到的问题: 在Linux虚拟机上部署一个tomcat同时在tomcat下放置2个工程,其 ...

  6. python实现建立soap通信(调用及测试webservice接口)

    实现代码如下: #调用及测试webservice接口 import requests class SoapConnect: def get_soap(self,url,data): r = reque ...

  7. 调用jersey发布的接口webservice,通过HttpPost传递文件

    项目媒体文件之前都是上传到七牛云处理,现在客户为了安全和私密性,准备将移动端拍摄的图片和视频传递到文件服务器,所以就想办法能不能在服务器端发布一个WebService,供移动端调用.刚好之前做的接口都 ...

  8. Java调用IIS发布的WebService

    之前的一篇博客说了一个实例,就是用VS2005在IIS上发布WebService.今天我们来实现在Eclipse上用Java来调用昨天发布的WebService. 首先咋在浏览器中输入http://1 ...

  9. WebService:java配置类形式发布WebService接口及遇见的问题总结

    配置WebService前需要以下依赖jar包 #版本只供参考,具体看项目 <dependency> <grouId>org.apache.cxf</grouId> ...

  10. Java借助axis2发布WebService

    Webservice: 1.Xml: 2.WSDL: Web service描述语言(WSDL)就是这样一个基于XML(标准通用标记语言下的一个子集)的语言,用于描述Web service及其函数.参 ...

随机推荐

  1. Oracle ADG 自动切换脚本分享

    为大家分享一个[Oracle ADG自动切换]的脚本,由云和恩墨工程师HongyeDBA编写,支持Switchover.Failover. 下载链接:https://www.modb.pro/down ...

  2. 21 如何写出一篇高质量的sci水文

    博客配套视频链接: https://www.bilibili.com/video/BV1fW4y1W7dS/ b 站直接看 模型确定, 结果正在跑(或已结束), 目标期刊已定,一般可以定顶刊 从目标期 ...

  3. jenkins 配置flyway报错No value provided for placeholder expressions: ${name}

    业务场景:使用flyway将一个数据库的变更同步到另一个数据库,数据同步到一半的时候报错 No value provided for placeholder expressions: ${name}. ...

  4. SQL注入利用及绕过总结

    SQL注入及绕过姿势总结 概述 SQL注入指用户输入的参数可控且没有被过滤,攻击者输入的恶意代码被传到后端与SQL语句一起构造并在数据库中执行 不同数据库的语法可能存在差异,以MySQL为例,其他差异 ...

  5. 16收16发ARINC429模块

    6通道发送, 16通道接收* 发送通道:每路发送通道FIFO大小为:511 x 32bit(CHR32216/32316) ,缓存256条发送消息(CHR32216-EX/32316-EX)发送FIF ...

  6. KubeSphere 使用 HTTPS 协议集成 Harbor 镜像仓库指南

    作者:申红磊,青云科技容器解决方案架构师,开源项目爱好者,KubeSphere Member. 上面两篇文章讲了如何部署 HTTPS Harbor 和对接 HTTP 的 Harbor 镜像仓库:接下来 ...

  7. 使用 KubeSphere 实现微服务的灰度发布

    前言 今天来说一说,在 KubeSphere 中两个 " 小姐姐 " 如何来回切换,这是什么意思哩?其实就是互联网产品中常用的灰度发布方式. 互联网产品需要快速迭代上线,既要保证新 ...

  8. 多模型COE方法

    1.概述 在当前的人工智能发展中,单一模型的表现往往难以满足复杂任务的需求.为应对这些挑战,多模型协作的方法应运而生,"专家组合"(Mixture of Experts)便是其中一 ...

  9. Uniswap V2 核心 合约代码

    Uniswap V2 核心 UniswapV2Factory UniswapV2Pair UniswapV2ERC20 IUniswapV2Router02 1. UniswapV2Factory 合 ...

  10. Jenkins Job触发其他远程Job

    https://blog.csdn.net/diaojian66/article/details/117334537 如果不想遇到连接远程Jenkins主机失败后的反复尝试,去掉认证会是一个不错的选择 ...