美文网首页
9.2WebService-CXF

9.2WebService-CXF

作者: 孔垂云 | 来源:发表于2017-05-25 01:32 被阅读0次

CXF也是WebService的一个实现框架,而且跟spring整合的非常好。下面讲一下cxf的主要实现方式

添加依赖pom.xml

<dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>apache-cxf</artifactId>
     <version>2.7.18</version>
      <type>pom</type>
</dependency>

这里选择的版本是2.7.18,不算新的版本,cxf由于依赖太多太多,而且每个版本升级,兼容性都比较差,一旦整合到既有系统,轻易不敢升级。这里需要根据需求,删减一些不用的jar

web.xml配置

<servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:cxf-beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

这里面有两部分,一个是cxf的配置,另一个是整合spring启动

接口定义类CxfService

@WebService
public interface CxfService {
    public String sayHello(String name);
}

主要是利用@WebService这个注解

接口实现类

@WebService()
public class CxfServiceImpl {
    public String sayHello(String name) {
        return "hello: " + name;
    }
}

主要是利用@WebService()这个注解。

spring配置cfx-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
    <jaxws:endpoint id="cxfService" implementor="com.critc.cxf.CxfServiceImpl" address="/cxfService"/>
</beans>

主要是利用jaxws这种方式,来发布WebService

都做完后,部署启动,在地址栏输入:http://localhost:8080/services

启动显示

查看WSDL

查看WSDL

动态调用

public class TestCxf {

    public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf
                .createClient("http://localhost:8080/services/cxfService?wsdl");
        // url为调用webService的wsdl地址
        QName name = new QName("http://cxf.critc.com/", "sayHello");
        // namespace是命名空间,methodName是方法名
        String xmlStr = "张三";
        // paramvalue为参数值
        Object[] objects;
        try {
            objects = client.invoke(name, xmlStr);
            System.out.println(objects[0].toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

需要四个参数,
wsdl的地址:http://localhost:8080/services/cxfService?wsdl
namespace:http://cxf.critc.com/
方法名:sayHello
参数列表:张三

然后就可以调用了

调用结果.png

CXF可以和Spring很好的整好到一起,而且CXF还有很多其他功能,并不单单是发布WebService这一个功能。有兴趣的可以仔细研究。

源码下载

本工程详细源码

相关文章

  • 9.2WebService-CXF

    CXF也是WebService的一个实现框架,而且跟spring整合的非常好。下面讲一下cxf的主要实现方式 添加...

网友评论

      本文标题:9.2WebService-CXF

      本文链接:https://www.haomeiwen.com/subject/medaxxtx.html