項目目錄
//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;@WebServicepublic 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 ,歡迎交流 : )