Java调用与发布Webservice接口(一)

Java调用与发布Webservice接口(一)

一 准备工作

(一)开发环境

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

springboot版本:

org.springframework.boot

spring-boot-starter-parent

2.7.8

cxf与httpclient 、hutool依赖:

org.apache.httpcomponents

httpclient

cn.hutool

hutool-all

5.8.20

二 发布接口

首先创建一个测试接口,@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 = "HTTP client请求/soapenv:Body>";

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 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 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接口,敬请期待。

🎎 相关推荐

一簡多繁辨析之「牠、它」→「它」
🎯 365bet娱乐app

一簡多繁辨析之「牠、它」→「它」

📅 07-22 👀 1506
如何让石头快速长青苔
🎯 365bet官网

如何让石头快速长青苔

📅 09-15 👀 9284
如何利用高级工具克隆任意网站
🎯 365bet地址

如何利用高级工具克隆任意网站

📅 08-08 👀 1060