(一)开发环境

发布时间:2025-06-24 21:11:56  作者:北方职教升学中心  阅读量:970


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

#serverserver.port=8085#cxf url:http://127.0.0.1:8085/ws/service?wsdlcxf.path=/ws。hutool依赖:

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

二 发布接口。wsdl", "ser:testService", "http://com.wp.webservice/service", param ); System.out.println(result); }。我在工作中遇到了带有head认证的webservice接口,cxf框架࿰刚刚开始使用c;头部参数认证在拦截器中进行#xff0c;但实际效果并不好,因此,

        调用结果:

        (二)hutool工具调用。

三  接口调用。

        这里就不赘述概念之类的࿰了c;直接进入正题。

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

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

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

@Override public String testService(String parameter) { // code return "parameter:" + parameter; }。展示两种最简单的方式,先说最简单的调用方法,httpclient方法调用,xml请求体需要使用soapui工具生成#xff1a;

        再创建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); }。

@WebService(name = "UnifySearchService", targetNamespace = "http://com.test.webservice/service")public interface UnifySearchService { @WebMethod() String testService(@WebParam(name = "parameter") String parameter);}。踩了很多坑,在这里分享记录。

        因为在最近的工作中接触到webservice接口,在探索Webservice的过程中,我们找到了上述两种方法,首先,hutool等依赖,以下内容如下::
        springbot版:

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.7.8</version>        <relativePath/></parent>

        cxf与httpclient 、

(一)调用httpclient。

        首先,

        另外,

        demo基于springbot,使用httpclient、调用webservice接口,也可以使用cxf提供的工具类来调用,具体的调用方法有时间再贴出来。@WebMethod()声明这是接口下的函数方法,@WebParam声明函数所需的参数。创建一个测试接口󿼌@Webservice声明这是webservice接口,name的接口名称,targetNamespace 非常重要的,显示webservice接口的命名空间。

        首先,

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

@Configurationpublic 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; }}。创建一个测试接口󿼌@Webservice声明这是webservice接口,name的接口名称,targetNamespace 非常重要,显示webservice接口的命名空间。wsdl", "", xmlParam); } catch (IOException e) { throw new RuntimeException(e); } System.out.println(result); }。

(一)开发环境。

        基于hutol提供的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", "hutol要求webservice接口"); String result = request("http://localhost:8085/ws/service?wsdl", "ser:testService", "http://com.wp.webservice/service", param ); System.out.println(result); }。

        在这里调用webservice接口,

        调用结果:

        除上述两种简单方法外,它可以适应所有情况,第二种方法需要进一步改进代码,代码࿰将在后续文章中发布c;包括如何使用hutool调用webservice接口和cxf动态调用webservice接口,敬请期待。

一  准备工作。