CXF开发WebService服务器端

开发必备
1.apache-cxf-2.2.6
2.spring-ws-1.5.8
3.eclipse-jee-galileo-SR1-win32

开发步骤:
一、新建一个Dynamic Web Project工程。名字叫WebService_CXF_Host
二、将apache-cxf-2.2.6 及 spring-ws-1.5.8 下的jar包拷贝到 WebService_CXF_Host 工程的lib文件夹内
三、建立entity包,并新建MyObject.java,内容如下:
package entity;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyObject", propOrder = {
    "id",
    "name"
})
public class MyObject {
    private int id;
    private String name;
    public int getId() {
           return id;
    }
    public void setId(int id) {
           this.id = id;
    }
    public String getName() {
           return name;
    }
    public void setName(String name) {
           this.name = name;
    }
  
}
请注意@XmlType内定义的field必须与类的field一一对应,否则服务器在启动时会报错!这一规则就说明,用于webservice使用的类最好不要与业务bean关系太密切。否则,一旦业务变化,就要花费精力思考是否会影响到webservice。

四、新建demo.spring包,并新建接口HelloWorld.java。内容如下:
package demo.spring;

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

import entity.MyObject;

@WebService
public interface HelloWorld {
    String stringInt(
           @WebParam(name = "text", targetNamespace = "http://spring.demo/") String text,
           @WebParam(name = "num", targetNamespace = "http://spring.demo/") int num);

    MyObject[] aryMyObjects(
           @WebParam(name = "myObjects", targetNamespace = "http://spring.demo/") MyObject[] myObjects);
}
请注意:@WebService是必须的;@WebParam不是必须的。如果没有@WebParam的描述,在wsdl文件内描述的方法中,参数名将变为arg0,arg1…以此类推.
另外,请留意aryMyObjects方法的返回值类型是数组。将来利用cxf自带的wsdl2java.bat命令生成的接口,返回值类型将是List<MyObject>

五、新建demo.spring.impl包,并接建HelloWorld的实现类HelloWorldImpl,内容如下:
package demo.spring;

import javax.jws.WebService;
import entity.MyObject;

@WebService(endpointInterface = "demo.spring.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

       public String stringInt(String text, int num) {
              System.out.print("stringInt called");
              return text+"   "+num;
       }

       public MyObject[] aryMyObjects(MyObject[] myObjects) {
              MyObject[] ary=new MyObject[myObjects.length];
              for(int i=0;i<ary.length;i++){
                     ary[i]=new MyObject();
                     ary[i].setId(myObjects[i].getId()+i);
                     ary[i].setName(myObjects[i].getName()+"  "+i);
              }
              return ary;
       }
}
请注意:@WebService是必须的

六、web.xml的内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
    <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>
       <display-name>CXF Servlet</display-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>/service/*</url-pattern>
    </servlet-mapping>
</web-app>

七、与web.xml同一文件夹内新建bean.xml,内容如下:
<beans xmlns="http://www.springframework.org/schema/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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <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="hello" class="demo.spring.impl.HelloWorldImpl" />

    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
       
</beans>

至此,CXF开发服务器端已经完成,可以在浏览器内输入
http://localhost:8080/WebService_CXF_Host/service/HelloWorld?wsdl查看发报的方法

你可能感兴趣的:(webservice,CXF)