一  准备工作

(一)开发环境

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. UEFI原理与编程(二)

    系统表 对UEFI应用程序和驱动程序开发人员来讲,系统表是最重要的数据结构之一,它是用户空间通往内核空间的通道.有了它,UEFI应用程序和驱动才可以访问UEFI内核.硬件资源和I/O设备. 1 在应用 ...

  2. meminfo一些容易混淆的点

    MemTotal: 688576 kB 总内存 MemFree: 153736 kB 空闲内存 MemAvailable: 339884 kB 可用内存 Buffers: 16 kB 给文件的缓冲大小 ...

  3. gaussian噪声

    高斯噪声 高斯噪声(Gaussian noise)是一种具有正态分布(也称作高斯分布)概率密度函数的噪声.换句话说,高斯噪声的值遵循高斯分布或者它在各个频率分量上的能量具有高斯分布.它被极其普遍地应用 ...

  4. 数据库周刊57丨Oracle 2021年度安全警报;MySQL 8.0.23发布;MySQL索引优化导致的死锁案例;巨杉数据库跨引擎事务实践;MongoDB企业级能力解析;OceanBase OBCP 实验指导手册……

    摘要:墨天轮数据库周刊第57期发布啦,每周1次推送本周数据库相关热门资讯.精选文章.干货文档. 热门资讯 1.Oracle 2021年度安全警报: Critical Patch Update 发布8个 ...

  5. 01-react的基本使用

    // 导入react和react-dom包 类似 vue 中的 import vue from 'vue' import react from 'react' // 内部的组件 import reac ...

  6. 22. uni-app 怎么跳转界面

    methods: { //gonavigate()为点击响应事件,可在HTML部分设置 @tap="gonavigate()" gonavigate(){ uni.navigate ...

  7. 016 Python 中的基本运算符

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Datatime:2022/7/28 15:01 # Filename:016 Python 中的基本运算 ...

  8. Android复习(六)核心组件—>Activity 任何和返回栈、进程和应用生命周期、Parcelable和Bundle

    了解任务和返回堆栈 任务是用户在执行某项工作时与之互动的一系列 Activity 的集合.这些 Activity 按照每个 Activity 打开的顺序排列在一个返回堆栈中.例如,电子邮件应用可能有一 ...

  9. Redis实现幂等、防抖、限流等功能

    本文章主要讲述如何使用Redis实现幂等.防抖.限流等功能. 幂等组件 import lombok.RequiredArgsConstructor; import org.springframewor ...

  10. lua中table中null的表示方法以及判断redis返回null

    今天遇到一个麻烦的问题,查询redis时候,查到数据的时候正常返回,查询不到数据时,返回了null,然而在lua中,常见的nil,但不常见null,这时候lua中对redis返回的null如何做判断呢 ...