Cxf Spring 整合技术开发
1. 下载相应的jar包(cxf和spring)
2. 创建一个工程,说明一下没有必要去创建webService的工程
3. 这里使用的JDK1.6 我在JDK1.5上不能(注意)
4. 将1步的jar包导入(copy)lib目录下
5. 配置web.xml文件
xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param> <param-name>contextConfigLocationparam-name> <param-value>/WEB-INF/beans.xmlparam-value> context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener listener-class> listener> <servlet> <servlet-name>CXFServletservlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet servlet-class> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>CXFServletservlet-name> <url-pattern>/*url-pattern> servlet-mapping> <welcome-file-list> <welcome-file>index.jspwelcome-file> welcome-file-list> a) web-app> |
6. 创建接口 (这里就拿最简单但是又有自定义的类)
|
代码:
package com.tchzt.ws.cxf.spring.cjoy.server;
import javax.jws.WebParam; import javax.jws.WebService;
import com.tchzt.ws.cxf.spring.cjoy.server.bean.Customer;
@WebService public interface HelloWorld {
String sayHello(@WebParam(name="customer")Customer customer); }
|
7. 创建自定义类
代码:
package com.tchzt.ws.cxf.spring.cjoy.server.bean;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Customer") public class Customer {
@XmlElement(nillable = true) private long id;
@XmlElement(nillable = true) private String name;
@XmlElement(nillable = true) private int age;
@XmlElement(nillable = true) private Date birthday;
//get、set方法.............
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public Date getBirthday() { return birthday; }
public void setBirthday(Date birthday) { this.birthday = birthday; }
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
|
8. 实现类
代码:
package com.tchzt.ws.cxf.spring.cjoy.server;
import javax.jws.WebService;
import com.tchzt.ws.cxf.spring.cjoy.server.bean.Customer;
@WebService(endpointInterface = "com.tchzt.ws.cxf.spring.cjoy.server.HelloWorld") public class HelloWorldImpl implements HelloWorld {
public String sayHello(Customer customer) {
return "hello ," +customer.getName(); }
}
|
9. 创建bean.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: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" />
<jaxws:endpoint id="helloWord" implementor="com.tchzt.ws.cxf.spring.cjoy.server.HelloWorldImpl" address="/HelloWord" />
<bean id="customer" class="com.tchzt.ws.cxf.spring.cjoy.server.bean.Customer"/>
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding"/>
<bean id="jaxws-and-aegis-service-factory" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"> <property name="dataBinding" ref="aegisBean"/> <property name="wrapped" value="true" /> bean>
beans>
|
10. 发布、驱动web服务(http://localhost:8089/cxfserver/HelloWord?wsdl)
11.
12. Wsdl文件内容
xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://server.cjoy.spring.cxf.ws.tchzt.com/" xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.cjoy.spring.cxf.ws.tchzt.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <wsdl:types> - <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://server.cjoy.spring.cxf.ws.tchzt.com/" xmlns="http://server.cjoy.spring.cxf.ws.tchzt.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> - <xs:complexType name="Customer"> - <xs:sequence> <xs:element name="id" nillable="true" type="xs:long" /> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string" /> <xs:element name="age" nillable="true" type="xs:int" /> <xs:element minOccurs="0" name="birthday" nillable="true" type="xs:dateTime" /> xs:sequence> xs:complexType> <xs:element name="sayHello" type="sayHello" /> - <xs:complexType name="sayHello"> - <xs:sequence> <xs:element minOccurs="0" name="customer" type="Customer" /> xs:sequence> xs:complexType> <xs:element name="sayHelloResponse" type="sayHelloResponse" /> - <xs:complexType name="sayHelloResponse"> - <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> xs:sequence> xs:complexType> xs:schema> wsdl:types> - <wsdl:message name="sayHello"> <wsdl:part element="tns:sayHello" name="parameters" /> wsdl:message> - <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters" /> wsdl:message> - <wsdl:portType name="HelloWorld"> - <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHello" name="sayHello" /> <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> wsdl:operation> wsdl:portType> - <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document" /> - <wsdl:input name="sayHello"> <soap:body use="literal" /> wsdl:input> - <wsdl:output name="sayHelloResponse"> <soap:body use="literal" /> wsdl:output> wsdl:operation> wsdl:binding> - <wsdl:service name="HelloWorldImplService"> - <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"> <soap:address location="http://localhost:8089/cxfserver/HelloWord" /> wsdl:port> wsdl:service> wsdl:definitions>
|
13. 第10步本来是不要贴出来 但是要强调一下 如果你在其他的位置配置出现错误得话它就不会出现自定义类的一下属性
14. Cxf Spring webservice服务器开发完成