当前位置 : 主页 > 网页制作 > Nodejs >

WebService技术总结(五):CXF整合Spring开发

来源:互联网 收集:自由互联 发布时间:2021-06-24
首先说说CXF和Spring整合以后的感想,在客户端调用方便了很多,和Spring紧密结合,获取SEI和普通的bean一样,不用再去写一大堆代码,实在很省心! 言归正传,接下来开始演示步骤 服务

首先说说CXF和Spring整合以后的感想,在客户端调用方便了很多,和Spring紧密结合,获取SEI和普通的bean一样,不用再去写一大堆代码,实在很省心!

言归正传,接下来开始演示步骤

服务端代码

1.新建web project,我的项目名为”csServer”,希望大家看的时候不要搞混,同样导入cxf的jar包

2.定义一个简单的接口及其实现类,和前几篇博客的接口类似
接口:

@WebService(targetNamespace="http://push.yonyou.com",
serviceName="PushData",
portName="PushPort",
name="PushPortType")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface PushData {
    public @WebResult(name="pk_org")String queryOrgs(@WebParam(name="corp")String corp) throws Exception;
}

实现类:

public class PushDataImp implements PushData{

private Map<String,String> infos = new HashMap<String,String>();

    public PushDataImp(){
        infos.put("01", "10011AFSD10");
        infos.put("02", "10011A87D10");
        infos.put("03", "10011A09E10");
    }

    @Override
    public String queryOrgs(String corp) throws Exception {
        String pk_org = infos.get(corp);
        if(pk_org == null)
            pk_org = "";
        return pk_org;
    }

}

4. 在src下新建bean-cxf.xml,配置服务端SEI,注意加入cxf的xsd,配置的大概内容:定义SEI接口bean,并将它注入到jaxws:server中

<?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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

    <!-- 定义接口及实现类 -->                       
    <bean id="pushService" class="com.yonyou.server.imp.PushDataImp"/>                      

    <jaxws:server address="/push" serviceClass="com.yonyou.server.PushData">
        <jaxws:serviceBean>
            <!-- 注入 -->
            <ref bean="pushService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

5.在web.xml中增加Spring监听器和CXF的Servlet,配置如下

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean-cxf.xml</param-value>
    </context-param>
    <!-- spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- cxf的servlet -->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

最后部署项目,启动tomcat服务器,输入cxf Servlet路径:
http://127.0.0.1:8080/csServer/ws
出现以下界面

可以看到所有基于SOAP协议和RESTful风格的WebService,点一下链接,就可以进入到相应的wsdl文件中

客户端代码

知道了wsdl的地址,接下来都是熟悉的步骤了
新建客户端的java project即可,我的项目名”csClient”,导入cxf的jar包


打开cmd,进入到客户端的src目录下,执行wsdl2java命令,因为我的jdk是1.6,所以依然要加“ -frontend jaxws21”,完整命令如下:
wsdl2java -d . -frontend jaxws21 http://127.0.0.1:8080/csServer/ws/push?wsdl

刷新项目,出现以下代码:


以上客户端的步骤和大多数web service类似,而和Spring整合之后,我们可以很方便的远程调用,首先新建一个Spring配置文件:bean-client.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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
            <jaxws:client id="queryData" address="http://127.0.0.1:8080/csServer/ws/push?wsdl" serviceClass="com.yonyou.push.PushPortType"/>
</beans>

只需要把当做一个普通的bean处理就好了,无非就是加了一个address属性,指定服务端wsdl文件路径

客户端调用:

public class Client {
    public static void main(String[] args) throws Exception_Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-client.xml");
        PushPortType push =  (PushPortType) ctx.getBean("queryData");
        String pk_org = push.queryOrgs("01");
        System.out.println(pk_org);
    }
}

可以看到,客户端的调用就是简单的获取了一个bean,调用其方法就可以,很方便,打印结果如下:

10011AFSD10
网友评论