English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french
查看: 6|回复: 0

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

[复制链接]
查看: 6|回复: 0

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

[复制链接]
查看: 6|回复: 0

397

主题

0

回帖

1201

积分

金牌会员

积分
1201
34345

397

主题

0

回帖

1201

积分

金牌会员

积分
1201
2025-2-7 00:58:35 | 显示全部楼层 |阅读模式

一  准备工作

(一)开发环境

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);}
创建该接口的实现类就可以在函数种编写业务处理代码:
@Overridepublic String testService(String parameter) {    // code        return "parameter:" + parameter;}创建发布webservice接口的配置文件:
@Configuration
public class CxfWebServiceConfig {
@Resourceprivate UnifySearchService unifySearchService;@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {    return new SpringBus();}/** * 访问地址 http://127.0.0.1:8085/ws/service?wsdl */@Beanpublic 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接口,敬请期待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

397

主题

0

回帖

1201

积分

金牌会员

积分
1201

QQ|智能设备 | 粤ICP备2024353841号-1

GMT+8, 2025-3-10 15:00 , Processed in 0.930631 second(s), 30 queries .

Powered by 智能设备

©2025

|网站地图