webService_cxf_消息中间件_5.16

前言:自上周五开始,因为公司刚好来人培训,因为我还在出差,熬了一个周末早9-晚9两天搞完了三天的课程,觉得还不错。。但是很多东西还是要自己摸索的。到今天来一个小结吧。

一.概述

1.webService 说白了,就是为企业执行SOA的一个工具而已。

2.消息中间件,作为SOA架构的一条esb,即企业服务总线。

二.实现

简介:这套东西要用到2个webservice(Webservice_Server&webService_Client),一个作为 hwHosting的webservice,一个作为jsClient的。在中间件上的webservice是通过webService_Server来重新部署一个新的地址,也就是说,我在调用java上的webService上时,数据会传到中间件上去。然后到中间件上的时候按照xslt---》javascript的方向来实现。

1.(webService_Server_for_hwHosting),主要是作为hwHosting的webservice,向中间件提供的部分。

@WebService
public interface HelloWorld {
	@WebMethod
    public String sayHello(@WebParam(name="name")String name);


}
public class HelloWorldImpl implements HelloWorld {

	public String sayHello(String name) {
		
		return "somethind";
	}
	public static void main(String[] args) {
	      HelloWorldImpl implementor = new HelloWorldImpl();
	      JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
	      svrFactory.setServiceClass(HelloWorld.class);
	      svrFactory.setAddress("http://127.0.0.1:9898/helloWorld");
	      svrFactory.setServiceBean(implementor);
	      svrFactory.getInInterceptors().add(new LoggingInInterceptor());
	      svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
	      svrFactory.create();
	   }
}

2.webService_Server_for_hwHosting,主要用来调用,测试服务

public class HelloClient {
	public static void main(String[] args) {   
	       JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
	       //factory.getInInterceptors().add(new LoggingInInterceptor());
	       factory.getOutInterceptors().add(new LoggingOutInterceptor());
	       factory.setServiceClass(HelloWorld.class);
	       factory.setAddress("http://127.0.0.1:1506/services/HelloWorldService.HelloWorldServiceHttpSoap11Endpoint");//这个地址是通过中间件的webService的wsdl文件中的endPoint中找到的
	       HelloWorld client = (HelloWorld) factory.create();
	       String reply = client.sayHello("2");
	       System.out.println("Server said: " + reply);
	       System.exit(0);
	    }
}

3.webService_Client_for_jsClient

@WebService
public interface IMultiMethod {
	
	@WebMethod
    public String testJs1(@WebParam(name="name")String name);
	
	@WebMethod
	public String testJs2(@WebParam(name="name")String name);
	
	@WebMethod
	public String testJs3(@WebParam(name="name")String name);
	
	@WebMethod
	public String testJs4(@WebParam(name="name")String name);
}

 

public class MultiMethodImpl implements IMultiMethod {

	public String testJs1(String name) {
		return "testJs1";
	}
	
	public String testJs2(String name) {
		return "testJs2";
	}
	
	public String testJs3(String name) {
		return "testJs3";
	}
	
	public String testJs4(String name) {
		return "testJs4";
	}
	
	public static void main(String[] args) {
		MultiMethodImpl implementor = new MultiMethodImpl();
	      JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
	      svrFactory.setServiceClass(IMultiMethod.class);
	      svrFactory.setAddress("http://127.0.0.1:2000/iMultiMethod");
	      svrFactory.setServiceBean(implementor);
	      svrFactory.getInInterceptors().add(new LoggingInInterceptor());
	      svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
	      svrFactory.create();
	   }

4.下面是一些测试数据。

  4.1传入的数据:一定要注意这里面有命名空间,再我处理的时候有很多的不便,所以我加入了一个xslt来去除    这个命名空间。

<ns2:sayHello xmlns:ns2="http://server.zxd.com/">//

<name>

2

</name>

</ns2:sayHello>

 4.2 xslt文件:一定要注意,在传过来的xml文件中有命名空间,所以我们的xslt文件也要有命名空间,如高亮区

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
xmlns:ns2="http://server.zxd.com/"
xmlns:i="http://www.w3.org/2001/XMLSchemainstance">
<xsl:template match="/ns2:sayHello">//match会选择在/ns:sayHello命名空间下的部分
<sayHello>
<name>
<xsl:value-of select="name" />
</name>
</sayHello>
</xsl:template>
</xsl:stylesheet>

  4.3 结果:如图所示,已经将原有的ns2去掉了。

<sayHello>
<name>
2
</name>
</sayHello>

5.下面继续看javascript的写法,主要是看我通过jsHosting传过来的数字,来判断,去调用jsClient的哪个具体的    方法

var next = output.append(input[0]);
var methodName=next.getField('/sayHello/name')
next.setProperty('methodName',methodName)
switch(methodName){
	case "1":next.setProperty("rhapsody:consumerOperation",'testJs1');break;
	case "2":next.setProperty("rhapsody:consumerOperation",'testJs2');break;
	case "3":next.setProperty("rhapsody:consumerOperation",'testJs3');break;
	case "4":next.setProperty("rhapsody:consumerOperation",'testJs4');break;
}

6.最终返回值:根据我传入的2,返回为testJs2,这样完成了所有的测试。

<ns2:testJs2Response>
<return>
testJs2
</return>
</ns2:testJs2Response>

三:总结

  Stop compliant,just code,just do something even if it maybe easy a lot....

很多东西都是在不段的积累中,幸福是来自于追求的路上,而不是获得以后。  

原文地址:https://www.cnblogs.com/weizizhe/p/3731723.html