spring简单整合cxf的服务端和客户端编写

一、服务端的编写
step0.模拟一个实体类Student.java

public class Student {
    private Long id;
    private String name;
    private String pwd;

    public Student(){};

    public Student(Long id, String name, String pwd) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    }
step1.编写一个提供服务的WebService接口
import javax.jws.WebMethod;
import javax.jws.WebService;

import cn.joe.entity.Student;

@WebService 
public interface StudentService {
    @WebMethod
    public Student getStudent();
}
step2.接口实现类
@WebService
public class StudentServiceImpl implements StudentService{

    @Override
    public Student getStudent() {
        Student student = new Student(1l,"张三","123");
        return student;
    }

}

step3.导入cxf中的包,编写服务端配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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"
    xmlns="http://www.springframework.org/schema/beans">

    <!-- 导入的是cxf3.1.16不用引入这些文件 -->
    <!-- <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="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
        <property name="wrapped" value="true" />
    </bean>
 -->
    <jaxws:endpoint id="studentService" address="/student"
        implementor="cn.joe.service.impl.StudentServiceImpl">

        <!-- <jaxws:serviceFactory>
            <ref bean="jaxWsServiceFactoryBean" />
        </jaxws:serviceFactory> -->
    </jaxws:ndpoint>
</beans>

step4.启动就行

二、客户端的编写
step1.通过wsdl2java url创建客户端代码
step2.创建xml文件beans-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:p="http://www.springframework.org/schema/p"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"   
    xmlns:cxf="http://cxf.apache.org/core"  
    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/schema/jaxws.xsd">  

     <bean id="studnetClient" class="cn.joe.service.StudentService" factory-bean="clientFactory"  
        factory-method="create" />  

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
        <property name="serviceClass" value="cn.joe.service.StudentService" />  
        <property name="address" value="http://localhost:8080/spring_ws/student" />      
    </bean> 
</beans> 

step3.编写测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.joe.service.Student;
import cn.joe.service.StudentService;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-client.xml");
        StudentService factory = (StudentService) context.getBean("studnetClient");

        Student student = factory.getStudent();
        System.out.println(student);

    }
}

step4.ok!

猜你喜欢

转载自blog.csdn.net/qiaoqiyu6416/article/details/81843030