最近看mule的一个文档mule esb 3 concepts,介绍了mule esb的一些基本概念。看完后,对soa,esb,服务等都有了更深的认识。今天试验通过mule进行cxf web service的调用,试了一下午终于成功。
1,首先要有一个已经发布的cxf web service,可以通过mule studio以图形化的方式简单生成config.xml,再写好相应的component class,就是发布用到的接口和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文件的方式调用。
在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环境中,创建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"//发出http request并等待response
host="localhost" port="8888">//从localhost:8888接收http request。
<response>
<object-to-string-transformer />//将响应返回给http transport,在浏览器回显。
</response>
</http:inbound-endpoint>
<http:outbound-endpoint exchange-pattern="request-response"//调用webservice并等待返回结果
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 是通过调用web service而得到的返回结果。