博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebService&CXF
阅读量:4699 次
发布时间:2019-06-09

本文共 4640 字,大约阅读时间需要 15 分钟。

WebService

• WebService通过HTTP POST方式接受客户的请求
• WebService与客户端之间一般使用SOAP协议传输XML数据
• 它本身就是为了跨平台或跨语言而设计的
网络上提供的服务:
http://webxml.com.cn/

XML 版 HTTP
SOAP 简单对象访问协议
SOAP作为一个基于XML语言的协议用于在网上传输数据。
SOAP = 在HTTP的基础上+XML数据。
SOAP是基于HTTP的。
SOAP的组成如下:
Envelope – 必须的部分。以XML的根元素出现。
Headers – 可选的。
Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。

WSDL web服务描述语言
就是一个文档 用于描述当前服务的一些信息
服务名称 service 标签的name
服务的发布地址 address 标签的location
服务提供懂得方法 operatioin 标签的name
方法的参数类型
方法的返回值类型

发布一个WebService服务
第一步:创建一个Java项目
第二步:创建一个类,加入Webservice注解
第三步:提供一个方法sayHello
第四步:在main方法中调用jdk提供的发布服务的方法
第五步:访问服务的wsdl文档(服务的发布地址+?wsdl)http://172.29.20.5:8080/hello?wsdl

1 package webservice; 2  3 import javax.jws.WebService; 4 import javax.xml.ws.Endpoint; 5  6 @WebService 7 public class HolleWebService { 8     public String sayHello(String name, int i) { 9         System.out.println("服务端的sayHello方法被调用了。。。。");10         return "helle" + name;11     }12 13     public static void main(String[] args) {14         String address = "http://172.29.20.5:8080/hello";15         Object implementor = new HolleWebService();16         Endpoint.publish(address, implementor);17     }18 }

 

jdk中wsimport命令使用

作用:解析wsdl文件,生成客户端本地代码
客户端调用
使用wsimport命令解析wsdl文件生成本地代码
通过本地代码创建一个代理对象
通过代理对象实现远程调用

package webservice;public class App {    public static void main(String[] args) {        HolleWebServiceService holleWebServiceService = new HolleWebServiceService();        HolleWebService proxy = holleWebServiceService.getHolleWebServicePort();        String sayHello = proxy.sayHello("stevezong", 1);        System.out.println(sayHello);    }}

CXF

支持多种协议:

• SOAP1.1,1.2
• XML/HTTP
• CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)
• 并可以与Spring进行快速无缝的整合
• 灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。
CXF 服务端 开发 spring + cxf
第一步:创建动态web项目
第二步:导入CXF相关jar包
第三步:在web.xml中配置CXF框架提供的一个Servlet
第四步:在类路径下提供cxf.xml
第五步:开发一个接口和实现类
第六步:在cxf.xml中注册服务
2
maven

maven
3.0.1
org.apache.cxf
cxf-rt-frontend-jaxws
${cxf.version}
org.apache.cxf
cxf-rt-transports-http
${cxf.version}
org.apache.cxf
cxf-rt-transports-http-jetty
${cxf.version}

 

3

wel
cxf
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
cxf
org.apache.cxf.transport.servlet.CXFServlet
config-location
classpath:cxf.xml
1
cxf
/service/*

 

4
cxf

 

5

java

package com.stevezong.cxf;import javax.jws.WebService;@WebServicepublic interface HelloService {    public String sayHello(String name);}
package com.stevezong.cxf;public class HelloServiceImpl implements HelloService {    public String sayHello(String name) {        System.out.println("cxf的sayHello");        return name + " name ";    }}

 

CXF 客户端

方式一:使用jdk提供的wsimport命令生成本地代码完成调用

方式二:使用CXF提供的方式(重点)

第一步:创建Java项目并导入CXF相关jar包
第二步:使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件
第三步:将接口文件复制到项目中
第四步:提供spring配置文件,注册客户端代理对象
第五步:读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用
1
maven
导包
2

C:\Program Files\Java\jdk1.8.0_111\bin>wsimport.exe -s \test http://127.0.0.1:8080/cxf/service/cfxService?wsdl正在解析 WSDL...正在生成代码...正在编译代码...

 

3

package com.stevezong.cxf;import javax.jws.WebService;@WebServicepublic interface HelloService {    public String sayHello(String name);}

 

4

 

5

package cxf;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test2 {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext("cxf2.xml");        HelloService proxy = (HelloService) ctx.getBean("myClient");        System.out.println(proxy.sayHello("stevezong"));    }}

 

转载于:https://www.cnblogs.com/lmdtx/p/9764966.html

你可能感兴趣的文章
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>
记一些从数学和程序设计中体会到的思想
查看>>
题目1462:两船载物问题
查看>>
POJ 2378 Tree Cutting(树形DP,水)
查看>>
第二冲刺阶段个人博客5
查看>>
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
MySQL 网络访问连接
查看>>
在aws ec2上使用root用户登录
查看>>
数据访问 投票习题
查看>>
cnblog!i'm coming!
查看>>
使用点符号代替溢出的文本
查看>>
Axios 中文说明
查看>>