webservice笔记

上传人:第*** 文档编号:32819358 上传时间:2018-02-12 格式:DOC 页数:8 大小:75.54KB
返回 下载 相关 举报
webservice笔记_第1页
第1页 / 共8页
webservice笔记_第2页
第2页 / 共8页
webservice笔记_第3页
第3页 / 共8页
webservice笔记_第4页
第4页 / 共8页
webservice笔记_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《webservice笔记》由会员分享,可在线阅读,更多相关《webservice笔记(8页珍藏版)》请在金锄头文库上搜索。

1、发布服务的几种方式: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 注解, 如果不加,虽然不报错,但是所有的方法都无法暴露. 与父类

2、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.getO

3、utInterceptors().add(new LoggingOutInterceptor();bean.create();System.out.println(hello Service ready.);3、使用 spring + Servlet 在 tomcat 中发布:cxf-servlet-spring.xml 配置文件中的内容 Web.xml 配置文件中的内容contextConfigLocationWEB-INF/cxf-servlet-spring.xmlorg.springframework.web.context.ContextLoaderListener CXFServl

4、etorg.apache.cxf.transport.servlet.CXFServlet1config-locationclasspath:cxf-servlet.xmlCXFServlet/*调用服务的几种方式:1、通过 jdk 的 wsimport 或者 CXF 的 wsdl2java 命令解析 WSDL 生成客户端代码,调用ws_Service 服务。wsimport -s . -p liwenlong.lwl http:/localhost:8080/JaxWsCXFHello?wsdl-s 生成源码 . 当前目录 -p 后面跟包名 指生成的源码所在的包 , 再后跟 wsdl 路径W

5、sdl2java -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)

6、;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/,HelloWorl

7、dPort);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_B

8、INDING,endpointAddress);/获取服务接口,调用方法HelloWorld helloWorld=service.getPort(HelloWorld.class);System.out.println(helloWorld.sayHello(Rodger );System.exit(0);2 QName service_name = new QName(http:/ HelloServiceService);QName port_name = new QName(http:/ HelloServicePort);/服务 wsdl 文档位置 URLURL wsdlUrl=ne

9、w 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 v

10、oid 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 的

11、 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);

12、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.mi

13、crosoft.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 放入 Tomc

14、at的 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();/指定 webservic

15、e 的 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(http:/ws.apache.org/axis2,getGreeting);/调用方法Object obj=serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)0;Logger.getLogger(lwl).info(obj.toString();classes=new Classint.class;opAddEntry=new QName(http:/ws.apache.org/axis2, getPrice);obj=serviceClient.invok

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号