CXF创建WS-随笔1

项目目录

CXF创建WS-随笔1_第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

//将自动为你创建客户端结构,我的结构如下

CXF创建WS-随笔1_第2张图片


//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 ,欢迎交流 : )



你可能感兴趣的:(java,String,service,url,features,Constructor)