WebService笔记.doc_第1页
WebService笔记.doc_第2页
WebService笔记.doc_第3页
WebService笔记.doc_第4页
WebService笔记.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

发布服务的几种方式:1、纯JDK发布ProjectManager projectManager=new ProjectManagerImpl();String address=http:/localhost:8080/ProjectManager;/参数1 服务的发布地址, 参数2 服务的实现者Endpoint.publish(address,projectManager);2、 使用JaxServiceFactoryBean 发布CXF的web服务,需要CXF框架jar包必须加入WebService注解,如果不加,虽然不报错,但是所有的方法都无法暴露. 与父类ServerFactoryBean发布方式相同JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();bean.setAddress(http:/localhost:8080/cxfhello);bean.setServiceClass(HelloServiceI.class);bean.setServiceBean(new HelloServiceImpl();/加入请求的消息拦截器bean.getInInterceptors().add(new LoggingInInterceptor();/加入输出的消息拦截器bean.getOutInterceptors().add(new LoggingOutInterceptor();bean.create();System.out.println(hello Service ready.);3、使用spring + Servlet 在tomcat中发布:cxf-servlet-spring.xml配置文件中的内容 Web.xml 配置文件中的内容 contextConfigLocation WEB-INF/cxf-servlet-spring.xml org.springframework.web.context.ContextLoaderListener CXFServlet org.apache.cxf.transport.servlet.CXFServlet 1 !- 通过初始化参数指定cxf配置文件的位置,默认为WEB-INF下的cxf-servlet.xml, 与上下文参数适用二选一 config-location classpath:cxf-servlet.xml - CXFServlet /* 调用服务的几种方式:1、 通过jdk的wsimport 或者 CXF的wsdl2java命令解析WSDL生成客户端代码,调用ws_Service服务。wsimport -s . -p liwenlong.lwl http:/localhost:8080/JaxWsCXFHello?wsdl-s 生成源码 . 当前目录 -p 后面跟包名 指生成的源码所在的包, 再后跟 wsdl路径Wsdl2java -d . -p liwenlong.lwl http:/localhost:8080/JaxWsCXFHello?wsdl-d 生成的文件目录, .代表当前目录 -p 包名 /对应WSDL 文档的 HelloServiceService helloServiceService=new HelloServiceService(); / 对应WSDL 文档的 HelloService helloService= helloServiceService.getHelloServicePort(); String say=helloService.sayHello(say hello); System.out.println(say); System.out.println(代理对象.+helloService.getClass().getName();2、 通过客户端编程的方式,调用服务端(该方法存在问题,不建议使用)1 /定义服务的服务名称和服务端口 import javax.xml.ws.soap.SOAPBinding; QName SERVCE_NAME=new QName(http:/helloworld.cxf.demo/,HelloWorld); QName PORT_NAME=new QName(http:/helloworld.cxf.demo/,HelloWorldPort); System.out.println(Starting Client); /用服务名称作为参数创建Service实例 Service service=Service.create(SERVCE_NAME); /定义服务地址 String endpointAddress=http:/localhost:8080/HelloWorld2; /SOAPBinding.SOAP11HTTP_BINDING 在javax.xml.ws.soap.SOAPBinding包中 /为服务创建新的端口 service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); /获取服务接口,调用方法 HelloWorld helloWorld=service.getPort(HelloWorld.class); System.out.println(helloWorld.sayHello(Rodger ); System.exit(0);2 QName service_name = new QName(/, HelloServiceService); QName port_name = new QName(/, HelloServicePort); /服务wsdl文档位置URL URL wsdlUrl=new URL(http:/localhost:8080/hello?wsdl); /创建服务实例 Service service = Service.create(wsdlUrl,service_name); /获得支持指定服务端点接口的对象代理实例 HelloService helloService = service.getPort(port_name, HelloService.class); String rtn = helloService.sayHello(bruce lee); System.out.println(rtn);3、 客户端在spring中配置:public static void main(String args) ApplicationContext context=new ClassPathXmlApplicationContext(com/client/ClientBean.xml);HiServiceI hs=(HiServiceI)context.getBean(HiService);String rtn=hs.sayHi(我是客户端);System.out.println(rtn);4、 需要在客户端加入CXFjar包,不建议这么做,可以用来测试服务端代码 public static void main(String args) /定义CXF的jaxwsProxyFactorybean 代理工厂,调用服务 JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean(); /设置代理工厂的属性,包括服务类和地址 factory.setAddress(http:/localhost:8080/loginAddress); factory.setServiceClass(LoginI.class); /获得服务的对象 LoginI loginI=(LoginI)factory.create(); /调用方法 Student stu=loginI.login(liwenlong,liwenlong); System.out.println(stu.getName(); System.out.println(客户端调用成功);/解决乱码问题 request.setCharacterEncoding(UTF-8); response.setCharacterEncoding(UTF-8); response.setContentType(text/html;charset=gbk); String titleNameBar=new String(request.getParameter(userName).getBytes(iso-8859-1),UTF-8);/SQL 驱动代码driver=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver:/localhost:1433;databaseName=dbuser=sapass=sa/MySQL 驱动代码driver=com.mysql.jdbc.Driverurl=jdbc:mysql:/localhost:3306/login2?useUnicode=true&characterEncoding=utf-8user=rootpassword=rootaxis2安装: 下载axis2的axis2-1.6.2-war文件,解压将axis2.war放入Tomcat的webapps目录中即可.发布服务: 将java文件编译为class文件,并将class文件放入axis2文件夹中WEB-INF文件夹下的pojo文件夹下,启动tomcat即可.访问webservice:下面的方式需要将axis2-1.6.2-binaxis2-1.6.2lib中jar导入项目/创建RPCServiceClient 对象RPCServiceClient serviceClient=new RPCServiceClient();Options options=serviceClient.getOptions();/指定webservice的URLEndpointReference targetEPR=new EndpointReference(http:/localhost:8080/axis2/services/SimpleService);options.setTo(targetEPR);/创建参数Object opAddEntryArgs=new Object李文龙,男;/返回的数据类型Class classes=new ClassString.class;/创建QName 指定命名空间,和需要调用的方法QName opAddEntry=new QName(/axis2,getGreeting);/调用方法Object obj=serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, class

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论