cxf服务端与客户端

前面的新建项目和导入相应的包省略。。。

1、编写web.xml,添加cxf

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

2、编写spring.xml

<beans 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:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://cxf.apache.org/jaxws 
            http://cxf.apache.org/schemas/jaxws.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <bean id="IBusinessWebService"
        class="com.whsp.BusinessWebServiceImpl">
    </bean>

    <jaxws:endpoint id="BusinessWebService"
        implementor="#IBusinessWebService" address="/BusinessWebService" />
        
</beans>

3、编写服务端,首先写一个接口类BusinessWebService

package com.whsp;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface BusinessWebService {

    @WebMethod
    @WebResult(name = "result")
    public String excuteBusinessPermissionUpdate(@WebParam(name = "xml")
    String xml) throws Exception;
    
}

再写这个接口的实现类BusinessWebServiceImpl

package com.whsp;

import javax.jws.WebService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

@WebService(endpointInterface = "com.whsp.BusinessWebService", name = "BusinessWebService", serviceName = "BusinessWebService", portName = "BusinessWebService")
public class BusinessWebServiceImpl implements BusinessWebService {
    

    @SuppressWarnings("unchecked")
    public String excuteBusinessPermissionUpdate(String xml) throws Exception {
        return "Hello World!";
    }
}

4、最后编写客户端MyServiceClient1类

package com.whsp;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

/**
 * 通过客户端提供的接口类调用
 * */
public class MyServiceClient1 {

    public static void main(String[] args) {
        //服务器地址
        String serviceUrl = "http://127.0.0.1:8888/readservice/services/BusinessWebService";
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(BusinessWebService.class);
        factory.setAddress(serviceUrl);
        BusinessWebService client = (BusinessWebService) factory
                .create();
  
        try {
            result = client.excuteBusinessPermissionUpdate("");
        } catch (Exception e) {
            e.printStackTrace();
        }
         System.out.println(result);
    }
    
    

}

最后部署运行得到输出结果:Hello World!


这是第一次用,就记录一下,其中有些地方也不怎么懂。以后养成习惯,学习了新的东西,都记录下来,这样可以增加印象,同时可以和大家交流交流。







































你可能感兴趣的:(cxf服务端与客户端)