mule 基于wsdl调用cxf web service
最近看mule的一個文檔mule esb 3 concepts,介紹了mule esb的一些基本概念。看完后,對soa,esb,服務(wù)等都有了更深的認識。今天試驗通過mule進行cxf web service的調(diào)用,試了一下午終于成功。
1,首先要有一個已經(jīng)發(fā)布的cxf web service,可以通過mule studio以圖形化的方式簡單生成config.xml,再寫好相應(yīng)的component class,就是發(fā)布用到的接口和pojo類。
interface
_____________________________
@WebService
public interface IHello {
@WebMethod
public String sayHello(@WebParam (name="name")String name);
}
?
class
_____________________
package test.server;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
?
package test.server;
public class Hello implements IHello {
public String sayHello(String name) {
// TODO Auto-generated method stub
System.err.println("name: "+name);
return "Hello, "+name;
}
}
2,用基于wsdl文件的方式調(diào)用。
在cxf的bin目錄,用wsdl2java生成client,就是下面繼承自javax.xml.ws.Service的java類。
wsdl2java命令:wsdl2java -d test -client http://localhost:8081?wsdl
?
生成的client類
————————————————————
package test.server;
?
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.0
?* 2011-12-18T15:35:33.461+08:00
?* Generated source version: 2.5.0
?*?
?*/
@WebServiceClient(name = "IHelloService",?
? ? ? ? ? ? ? ? ? wsdlLocation = "http://localhost:8081?wsdl",
? ? ? ? ? ? ? ? ? targetNamespace = "http://server.test/")?
public class IHelloService extends Service {
?
? ? public final static URL WSDL_LOCATION;
?
? ? public final static QName SERVICE = new QName("http://server.test/", "IHelloService");
? ? public final static QName IHelloPort = new QName("http://server.test/", "IHelloPort");
? ? static {
? ? ? ? URL url = null;
? ? ? ? try {
? ? ? ? ? ? url = new URL("http://localhost:8081?wsdl");
? ? ? ? } catch (MalformedURLException e) {
? ? ? ? ? ? java.util.logging.Logger.getLogger(IHelloService.class.getName())
? ? ? ? ? ? ? ? .log(java.util.logging.Level.INFO,?
? ? ? ? ? ? ? ? ? ? ?"Can not initialize the default wsdl from {0}", "http://localhost:8081?wsdl");
? ? ? ? }
? ? ? ? WSDL_LOCATION = url;
? ? }
?
? ? @WebEndpoint(name = "IHelloPort")
? ? public IHello getIHelloPort() {
? ? ? ? return super.getPort(IHelloPort, IHello.class);
? ? }
?
}
3,eclipse+mule ide環(huán)境中,創(chuàng)建mule project,將上面的client類copy到project,再新建一個config.xml。然后右鍵選擇運行。
config.xml如下:
?
<?xml version="1.0" encoding="UTF-8"?>
?
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd?
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd?
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
<flow name="client">
<http:inbound-endpoint exchange-pattern="request-response"//發(fā)出http request并等待response
host="localhost" port="8888">//從localhost:8888接收http request。
?
<response>
<object-to-string-transformer />//將響應(yīng)返回給http transport,在瀏覽器回顯。
</response>
</http:inbound-endpoint>
?
<http:outbound-endpoint exchange-pattern="request-response"//調(diào)用webservice并等待返回結(jié)果
host="localhost" port="8081">
<cxf:jaxws-client port="IHelloPort" clientClass="test.server.IHelloService"
operation="sayHello" wsdlLocation="http://localhost:8081?wsdl" />
</http:outbound-endpoint>
</flow>
</mule>
?4,運行config.xml.在地址欄輸入http://localhost:8888/abc, browser會顯示hello,/abc.
hello,/abc 是通過調(diào)用web service而得到的返回結(jié)果。
總結(jié)
以上是生活随笔為你收集整理的mule 基于wsdl调用cxf web service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts2之单个文件上传(特别推荐)
- 下一篇: 银行能换零钱吗