1. 程式人生 > >spring簡單整合cxf的服務端和客戶端編寫

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!