在开源ESB中最活跃的就是Mule和ServiceMix了。为了便于大家更好的了解Mule和CXF,在此我通过一个实例来说明如何在Mule中使用CXF。
1.相关类
public class Product implements Serializable {
private String id;
private String description;
private int width;
private int height;
public Product() {
}
public Product(String id, String description, int width, int height) {
this.id = id;
this.description = description;
this.width = width;
this.height = height;
}
//相关的set,get方法。省略
}
@WebService
public interface ProductCatalogService {
@WebResult(name="item")
public List<String> listProducts();
@WebResult(name="product")
public Product getProductDetail(@WebParam(name="productId") String productId);
}
@WebService(endpointInterface = "com.honno.demo.product.ProductCatalogService",
serviceName = "ProductCatalogService")
public class ProductCatalogServiceImpl implements ProductCatalogService {
Map<String, Product> productMap = new HashMap<String, Product>();
public ProductCatalogServiceImpl() {
// Load some products
Product product = new Product("product1", "Square Widget", 10, 10);
productMap.put(product.getId(), product);
product = new Product("product2", "Round Widget", 5, 5);
productMap.put(product.getId(), product);
}
public List<String> listProducts() {
List<String> productListing = new ArrayList<String>();
Collection<Product> products = productMap.values();
for (Product p : products) {
productListing.add(p.getId() + " - " + p.getDescription());
}
return productListing;
}
public Product getProductDetail(String productId) {
Product product = null;
product = productMap.get(productId);
return product;
}
2.mule的配置文件
<spring:beans>
<spring:import resource="catalogContext.xml"/>
</spring:beans>
<model name="services">
<service name="ProductCatalogService">
<inbound>
<cxf:inbound-endpoint address="http://localhost:65082/services/ProductCatalogService" />
</inbound>
<component>
<spring-object bean="productCatalogService" />
</component>
</service>
</model>
3.生成wsdl文件
Mule成功启动后,在ie的地址栏中通过http://localhost:65082/services/ProductCatalogService?wsdl即可查看wsdl文件。
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://product.demo.honno.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ProductCatalogService" targetNamespace="http://product.demo.honno.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://product.demo.honno.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://product.demo.honno.com/">
<xs:complexType name="product">
<xs:sequence>
<xs:element minOccurs="0" name="description" type="xs:string"/>
<xs:element name="height" type="xs:int"/>
<xs:element minOccurs="0" name="id" type="xs:string"/>
<xs:element name="width" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="listProducts" type="listProducts"/>
<xs:complexType name="listProducts">
<xs:sequence/>
</xs:complexType>
<xs:element name="listProductsResponse" type="listProductsResponse"/>
<xs:complexType name="listProductsResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="item" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getProductDetail" type="getProductDetail"/>
<xs:complexType name="getProductDetail">
<xs:sequence>
<xs:element minOccurs="0" name="productId" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getProductDetailResponse" type="getProductDetailResponse"/>
<xs:complexType name="getProductDetailResponse">
<xs:sequence>
<xs:element minOccurs="0" name="product" type="product"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getProductDetailResponse">
<wsdl:part element="tns:getProductDetailResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="listProductsResponse">
<wsdl:part element="tns:listProductsResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="listProducts">
<wsdl:part element="tns:listProducts" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getProductDetail">
<wsdl:part element="tns:getProductDetail" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="ProductCatalogService">
<wsdl:operation name="listProducts">
<wsdl:input message="tns:listProducts" name="listProducts">
</wsdl:input>
<wsdl:output message="tns:listProductsResponse" name="listProductsResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getProductDetail">
<wsdl:input message="tns:getProductDetail" name="getProductDetail">
</wsdl:input>
<wsdl:output message="tns:getProductDetailResponse" name="getProductDetailResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ProductCatalogServiceSoapBinding" type="tns:ProductCatalogService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="listProducts">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="listProducts">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="listProductsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getProductDetail">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getProductDetail">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getProductDetailResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ProductCatalogService">
<wsdl:port binding="tns:ProductCatalogServiceSoapBinding" name="ProductCatalogServiceImplPort">
<soap:address location="http://localhost:65082/services/ProductCatalogService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
4.生成客户端
通过wsdl我们就可以生成客户端代码,对webservice进行调用了。
更多内容,包括更加详细的源代码,见[url] http://www.opensourceforce.org/?fromuid=217 [/url] (Mule的版块中可以找到相关代码)。
欢迎大家交流。
1.相关类
public class Product implements Serializable {
private String id;
private String description;
private int width;
private int height;
public Product() {
}
public Product(String id, String description, int width, int height) {
this.id = id;
this.description = description;
this.width = width;
this.height = height;
}
//相关的set,get方法。省略
}
@WebService
public interface ProductCatalogService {
@WebResult(name="item")
public List<String> listProducts();
@WebResult(name="product")
public Product getProductDetail(@WebParam(name="productId") String productId);
}
@WebService(endpointInterface = "com.honno.demo.product.ProductCatalogService",
serviceName = "ProductCatalogService")
public class ProductCatalogServiceImpl implements ProductCatalogService {
Map<String, Product> productMap = new HashMap<String, Product>();
public ProductCatalogServiceImpl() {
// Load some products
Product product = new Product("product1", "Square Widget", 10, 10);
productMap.put(product.getId(), product);
product = new Product("product2", "Round Widget", 5, 5);
productMap.put(product.getId(), product);
}
public List<String> listProducts() {
List<String> productListing = new ArrayList<String>();
Collection<Product> products = productMap.values();
for (Product p : products) {
productListing.add(p.getId() + " - " + p.getDescription());
}
return productListing;
}
public Product getProductDetail(String productId) {
Product product = null;
product = productMap.get(productId);
return product;
}
2.mule的配置文件
<spring:beans>
<spring:import resource="catalogContext.xml"/>
</spring:beans>
<model name="services">
<service name="ProductCatalogService">
<inbound>
<cxf:inbound-endpoint address="http://localhost:65082/services/ProductCatalogService" />
</inbound>
<component>
<spring-object bean="productCatalogService" />
</component>
</service>
</model>
3.生成wsdl文件
Mule成功启动后,在ie的地址栏中通过http://localhost:65082/services/ProductCatalogService?wsdl即可查看wsdl文件。
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://product.demo.honno.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ProductCatalogService" targetNamespace="http://product.demo.honno.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://product.demo.honno.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://product.demo.honno.com/">
<xs:complexType name="product">
<xs:sequence>
<xs:element minOccurs="0" name="description" type="xs:string"/>
<xs:element name="height" type="xs:int"/>
<xs:element minOccurs="0" name="id" type="xs:string"/>
<xs:element name="width" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="listProducts" type="listProducts"/>
<xs:complexType name="listProducts">
<xs:sequence/>
</xs:complexType>
<xs:element name="listProductsResponse" type="listProductsResponse"/>
<xs:complexType name="listProductsResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="item" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getProductDetail" type="getProductDetail"/>
<xs:complexType name="getProductDetail">
<xs:sequence>
<xs:element minOccurs="0" name="productId" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="getProductDetailResponse" type="getProductDetailResponse"/>
<xs:complexType name="getProductDetailResponse">
<xs:sequence>
<xs:element minOccurs="0" name="product" type="product"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getProductDetailResponse">
<wsdl:part element="tns:getProductDetailResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="listProductsResponse">
<wsdl:part element="tns:listProductsResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="listProducts">
<wsdl:part element="tns:listProducts" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getProductDetail">
<wsdl:part element="tns:getProductDetail" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="ProductCatalogService">
<wsdl:operation name="listProducts">
<wsdl:input message="tns:listProducts" name="listProducts">
</wsdl:input>
<wsdl:output message="tns:listProductsResponse" name="listProductsResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getProductDetail">
<wsdl:input message="tns:getProductDetail" name="getProductDetail">
</wsdl:input>
<wsdl:output message="tns:getProductDetailResponse" name="getProductDetailResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ProductCatalogServiceSoapBinding" type="tns:ProductCatalogService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="listProducts">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="listProducts">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="listProductsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getProductDetail">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getProductDetail">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getProductDetailResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ProductCatalogService">
<wsdl:port binding="tns:ProductCatalogServiceSoapBinding" name="ProductCatalogServiceImplPort">
<soap:address location="http://localhost:65082/services/ProductCatalogService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
4.生成客户端
通过wsdl我们就可以生成客户端代码,对webservice进行调用了。
更多内容,包括更加详细的源代码,见[url] http://www.opensourceforce.org/?fromuid=217 [/url] (Mule的版块中可以找到相关代码)。
欢迎大家交流。