在synapse(ESB)下应用XSLT进行信息转换

1:应用axis2创建WebServices.
public class SimpleService
{
    public String getGreeting(String name)
    {
        return "你好 " + name;
    }    
    public int getPrice()
    {
        return new java.util.Random().nextInt(1000);
    }    
}


2:配置Synapse:
synapse.xml:
<definitions xmlns="http://ws.apache.org/ns/synapse">
	<registry provider="org.wso2.carbon.mediation.registry.ESBRegistry">
        <parameter name="root">file:repository/samples/resources/</parameter>
        <parameter name="cachableDuration">15000</parameter>
    </registry>
    <proxy name="SimpleService">
        <target>
            <endpoint>
                <address uri="http://localhost:8080/axis2/services/SimpleService"/>
            </endpoint>
            <outSequence>
				<out>
					<xslt key="transform/transform_back.xslt"/>
				</out>
                <send/>
            </outSequence>
			
			<send/>
        </target>
        <publishWSDL uri="http://localhost:8080/axis2/services/SimpleService?wsdl"/>
    </proxy>
	<filter source="get-property('To')" regex=".*/SimpleService.*">
		<send>
			<endpoint>
				<address uri="http://localhost:8080/axis2/services/SimpleService"/>
			</endpoint>
		</send>
		<drop/>
	</filter>
	<send/>
</definitions>


transform_back.xslt
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
	<xsl:template match="/">
		<ns:getGreetingResponse xmlns:ns="http://ws.apache.org/axis2">  
			 <return>I_WAS_TRANSFORMED</return>  
		</ns:getGreetingResponse>   
	</xsl:template>

</xsl:stylesheet>


3:实现客户端:
public class MyClient
{
    public static void main(String[] args) throws Exception  
    {
        //  使用RPC方式调用WebService        
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        //  指定ESB的URL
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8280/services//SimpleService");
        options.setTo(targetEPR);
        //  指定getGreeting方法的参数值
        Object[] opAddEntryArgs = new Object[] {"test"};
        //  指定getGreeting方法返回值的数据类型的Class对象
        Class[] classes = new Class[] {String.class};
        //  指定要调用的getGreeting方法及WSDL文件的命名空间
        QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting");
        //  调用getGreeting方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
      
    } 
}

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