作者:傅笑寒 | 来源:互联网 | 2023-09-24 20:56
https:www.jianshu.comp527d3ba6ad14 一、概述webservice其实是个类似文档的东西,保证不同平台应用之间可以相互操作,之前都是使用java拓展
https://www.jianshu.com/p/527d3ba6ad14
一、概述
webservice其实是个类似文档的东西,保证不同平台应用之间可以相互操作,之前都是使用java拓展包进行ws发布,生成一个代理jar进行调用,这次试试springmvc+cxf,不过这个还是soap方式的,restful方式的需要研究一下jersy再放出来。
二、实例
1.pom文件中引入cxf
>
>org.apache.cxf>
>cxf-rt-frontend-jaxws>
>2.6.0>
>
>
>org.apache.cxf>
>cxf-rt-transports-http>
>2.6.0>
>
2.webservice 接口 及实现类
package com.web.cxf.service;
import javax.jws.WebService;
@WebService
public interface CXFWebService {
public String hello(String text);
}
package com.web.cxf.service.impl;
import com.web.cxf.service.CXFWebService;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(endpointInterface = "com.web.cxf.service.CXFWebService",
targetNamespace = "http://service.cxf.web.com/",
serviceName = "CXFWebService")
public class CXFWebServiceImpl implements CXFWebService{
public String hello(@WebParam String text) {
return "Hello " + text;
}
}
3.配置applicationContext-cxf.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:mvc="http://www.alibaba.com/schema/stat"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd
http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<context:annotation-config />
id="test" class="com.web.cxf.service.impl.CXFWebServiceImpl" />
>
4.添加xml到web入口
5.启动项目查看http://localhost:8080/cxf/testCXF?wsdl
6.客户端调用
package com.web.cxf.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import javax.xml.namespace.QName;
public class CXFWebServiceClient {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/cxf/testCXF?wsdl");
Object[] objects;
查看控制台
-end-
作者:榆次丶笑笑生
链接:https://www.jianshu.com/p/527d3ba6ad14
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。