CXF創建WS-隨筆1

項目目錄



//cxf2.5.2

//=====================服務器端程序================

//接口

//com.jimmy.ws.IPersonService

package com.jimmy.ws;


import java.util.List;


import javax.jws.WebParam;
import javax.jws.WebService;


import com.jimmy.pojo.Person;
@WebService
public interface IPersonService {
public List<Person> findAll(@WebParam(name = "arg0") String name);
}



//接口實現類

//com.jimmy.ws.PersonServiceImp

package com.jimmy.ws;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import com.jimmy.pojo.Person;
@WebService(endpointInterface="com.jimmy.ws.IPersonService",serviceName="person")
public class PersonServiceImp implements IPersonService{
	public List<Person> findAll( String name){
		ArrayList<Person> persons = new ArrayList<Person>();
		System.out.println(name);
		System.out.println("Server return all persons");
		for(int i=0;i<5;i++){
			Person p = new Person();
			p.setAge(i+18);
			p.setName("my name is Xman-"+i);
			persons.add(p);
		}
		return persons;
	}
}



//POJO


package com.jimmy.pojo;

public class Person {
private String name;
private int age;

public Person(){}
public Person(String name, int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


}



//spring配置文件bean.xml

<?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" >


    <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="serviceimp"
        address="/person"
        implementor="com.jimmy.ws.PersonServiceImp">


       <jaxws:serviceFactory>
        <ref bean="jaxWsServiceFactoryBean"/>
       </jaxws:serviceFactory>
    </jaxws:endpoint>
</beans>


//web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
    <display-name>cxfTest</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener   
        </listener-class>
    </listener>
    <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>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>



//至此,服務器端應用就生成好了

//部署到TOMCAT run起來

//對應的WSDL地址就是http://localhost:8080/test/ws/person?wsdl

//瀏覽器中打開上述WSDL地址,下載下來,注意後綴改爲WSDL,我這裏是person.wsdl


//=====================客戶器端程序================
//命令行進入->cxf安裝目錄/bin

//輸入 wsdl2java -client [wsdl安裝路徑]/person.wsdl

//將自動爲你創建客戶端結構,我的結構如下



//eclipse中創建一個java項目,並將上述生成的目錄代碼拷貝到項目SRC下,最好將wsdl也考到項目java文件同級中(我的放在外面絕對路徑貌似有問題)


//拷貝完成後,如果在Person_Service.java文件中以下代碼報錯,可以先註釋掉

public Person_Service(WebServiceFeature ... features) {
        super(WSDL_LOCATION, SERVICE, features);
    }


    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
    //compliant code instead.
    public Person_Service(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }


    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
    //compliant code instead.
    public Person_Service(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
        super(wsdlLocation, serviceName, features);
    }


//好了,完成

//進入IPersonService_PersonServiceImpPort_Client.java  run一下

//如果報錯,信息裏有Can not initialize the default wsdl from... 那麼表示你的WSDL路徑不正確,這就是我前面提到的"最好將wsdl也考到項目java文件同級中(我的放在外面絕對路徑貌似有問題)",考到同級後,把Person_Service.java文件中所有絕對路徑的WSDL地址改爲person.wsdl,去掉前面的路徑,當然如果你的路徑沒有問題,沒報錯,可以忽略下列信息



//改後的Person_Service.java文件

package com.jimmy.ws;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;

/**
 * This class was generated by Apache CXF 2.5.2
 * 2012-02-24T17:07:12.431+08:00
 * Generated source version: 2.5.2
 * 
 */
@WebServiceClient(name = "person", 
                  wsdlLocation = "person.wsdl",
                  targetNamespace = "http://ws.jimmy.com/") 
public class Person_Service extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://ws.jimmy.com/", "person");
    public final static QName PersonServiceImpPort = new QName("http://ws.jimmy.com/", "PersonServiceImpPort");
    static {
        URL url = Person_Service.class.getResource("person.wsdl");
        if (url == null) {
            java.util.logging.Logger.getLogger(Person_Service.class.getName())
                .log(java.util.logging.Level.INFO, 
                     "Can not initialize the default wsdl from {0}", "person.wsdl");
        }       
        WSDL_LOCATION = url;
    }

    public Person_Service(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public Person_Service(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public Person_Service() {
        super(WSDL_LOCATION, SERVICE);
    }
    
    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
    //compliant code instead.
   /* public Person_Service(WebServiceFeature ... features) {
        super(WSDL_LOCATION, SERVICE, features);
    }

    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
    //compliant code instead.
    public Person_Service(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }

    //This constructor requires JAX-WS API 2.2. You will need to endorse the 2.2
    //API jar or re-run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1
    //compliant code instead.
    public Person_Service(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
        super(wsdlLocation, serviceName, features);
    }*/

    /**
     *
     * @return
     *     returns IPersonService
     */
    @WebEndpoint(name = "PersonServiceImpPort")
    public IPersonService getPersonServiceImpPort() {
        return super.getPort(PersonServiceImpPort, IPersonService.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns IPersonService
     */
    @WebEndpoint(name = "PersonServiceImpPort")
    public IPersonService getPersonServiceImpPort(WebServiceFeature... features) {
        return super.getPort(PersonServiceImpPort, IPersonService.class, features);
    }

}


QQ:390887309 ,歡迎交流 : )



發佈了20 篇原創文章 · 獲贊 52 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章