WebService远程调用技术--学习02
来源:互联网 发布:lte数据功能收费吗 编辑:程序博客网 时间:2024/05/18 03:11
CXF介绍、安装和配置
1.1CXF介绍
CXF是一个开源的webservice框架,提供很多完善功能,可以实现快速开发
CXF支持的协议:SOAP1.1/1.2,REST
CXF支持数据格式:XML,JSON(仅在REST方式下支持)
1.2安装和配置
下载地址:http://cxf.apache.org/download.html
包结构介绍:
安装和配置:
第一步:安装JDK,建议1.7
第二步:解压apache-cxf-3.1.14.zip到指定目录,配置环境变量,创建CXF_HOME
第三步:把CXF_HOME加入到Path路径下
第四步:测试,在cmd下加入wsdl2java –h
CXF发布SOAP协议的服务
服务端: 1.导入Jar包 2.创建SEI接口,要加入@WebService @WebService public interface WeatherInterface { String queryWeather(String cityName); } 3.创建SEI实现类 public class WeatherInterfaceImpl implements WeatherInterface{ @Override public String queryWeather(String cityName) { System.out.println("from client..." + cityName); if("上海".equals(cityName)){ return "冷"; }else { return "热"; } } } 4.发布服务, JaxWsServerFactoryBean发布,设置3个参数,1.服务接口;2.服务实现类;3.服务地址;endpoint仅支持发布实现类,JaxWsServerFactoryBean支持发布接口。 public class WeatherServer { public static void main(String[] args) { //JaxWsServerFactoryBean发布服务 JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean(); //设置服务接口 jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class); //设置服务实现类 jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl()); //设置服务地址 jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather"); //设置拦截器必须加到服务端,在服务端发布之前,获取拦截器列表,将自己的拦截器加入列表中 jaxWsServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); jaxWsServerFactoryBean.getOutFaultInterceptors().add(new LoggingOutInterceptor()); //发布 jaxWsServerFactoryBean.create(); } } 5.测试服务是否发布成功,阅读使用说明书,确定关键点。如果在CXF发布的服务下,直接访问服务地址,会如下异常:No binding operation info while invoking unknown method with params unknown. 此时直接访问使用说明书地址即可客户端: 1.生成客户端代码,命令为 wsimport -p cn.ws.cxf -s . http://127.0.0.1:12345/weather?wsdl Wsdl2java命令是CXF提供的生成客户端的工具,他和wsimport类似,可以根据WSDL生成客户端代码 Wsdl2java常用参数: -d,指定输出目录 -p,指定包名,如果不指定该参数,默认包名是WSDL的命名空间的倒序 Wsdl2java支持SOAP1.1和SOAP1.2 2.使用说明书,使用生成代码调用服务端,JaxWsProxyFactoryBean调用服务端,设置2个参数,1.设置服务接口;2.设置服务地址。public class WeatherClient { public static void main(String[] args) { //JaxWsPorxyFactoryBean调用服务器 JaxWsProxyFactoryBean jaxWsPorxyFactoryBean = new JaxWsProxyFactoryBean(); //设置服务接口 jaxWsPorxyFactoryBean.setServiceClass(WeatherInterface.class); //设置服务地址 jaxWsPorxyFactoryBean.setAddress("http://127.0.0.1:12345/weather"); //获取服务接口实例 WeatherInterface weatherInterface = jaxWsPorxyFactoryBean.create(WeatherInterface.class); //调用查询方法 String queryWeather = weatherInterface.queryWeather("上海"); System.out.println(queryWeather); }}
3.CXF+Spring整合发布SOAP的服务
1.创建web项目(引入jar包)2.创建SEI接口3.创建SEI实现类4.配置spring配置文件,applicationContext.xml,用<jaxws:server标签发布服务,设置1.服务地址;2.设置服务接口;3设置服务实现类<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:server>发布SOAP协议的服务,对JaxWsServerFactoryBean类封装,设置1.服务地址;2.设置服务接口;3设置服务实现类 --> <jaxws:server address="/weather" serviceClass="cn.ws.cxf.server.WeatherInterface"> <jaxws:serviceBean> <ref bean="weatherInterface"/> </jaxws:serviceBean> <!-- 配置拦截器 --> <jaxws:inInterceptors> <ref bean="inIntercepter"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <ref bean="outIntercepter"/> </jaxws:outInterceptors> </jaxws:server> <!-- 配置服务实现类 --> <bean name="weatherInterface" class="cn.ws.cxf.server.WeatherInterfaceImpl"/> <!-- 配置拦截器的bean --> <bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> <bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean></beans>5.配置web.xml,配置spring配置文件地址和加载的listener,配置CXF的servlet<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置spring配置文件地址和加载的listener,配置CXF的servlet --> <!-- 设置spring的环境 --> <context-param> <!-- contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping></web-app>6.部署到tomcat下,启动tomcat7.测试服务,阅读使用说明书 WSDL地址规则:http://ip:端口号/项目名称/servlet拦截路径/服务名称?wsdl客户端: 1.引入jar包 2.生成客户端代码 3.配置spring配置文件,applicationContent.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:client>实现客户端,对JaxWsProxyFactoryBean类封装 --> <jaxws:client id="weatherClient" address="http://127.0.0.1:8080/ws_2_cxf_spring_server/ws/weather" serviceClass="cn.ws.cxf.WeatherInterface"/> </beans> 4.配置web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置spring配置文件地址和加载的listener,配置CXF的servlet --> <!-- 设置spring的环境 --> <context-param> <!-- contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping></web-app> 5.从spring上下文件获取服务实现类,调用查询方法,打印 public class WeatherClient { public static void main(String[] args) { //初始化spring的上下文 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml"); WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient"); String weather = weatherInterface.queryWeather("上海"); System.out.println(weather); } }
4.CXF发布REST服务
1.什么是REST 定义:REST就是一种编程风格,它可以精确定位网上资源(服务接口、方法、参数) REST支持数据格式:XML、JSON REST支持发送方式:GET,POST服务端: 1.导入jar包 2.创建学生pojo类,要加入@ XmlRootElement @XmlRootElement(name="student")//@XmlRootElement可以实现对象和XML数据之间的转换 public class Student { private long id; private String name; private Date birthday; //生产getXXX()和setXXX()... } 3.创建SEI接口 @WebService @Path("/student")//@Path("/student")就是将请求路径中的"/student"映射到接口上 public interface StudentInterface { //查询单个学生 @GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST) @Produces(MediaType.APPLICATION_XML)//指定服务数据类型 @Path("/query/{id}")//@Path("/query/{id}")就是将"/query"映射到方法上,"{id}"映射到参数上,多个参数,以"/"隔开,放到"{}"中 Student query(@PathParam("id")long id); //查询多个学生 @GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST) @Produces(MediaType.APPLICATION_JSON)//指定服务数据类型 @Path("/queryList/{name}")//@Path("/queryList/{name}")就是将"/queryList"映射到方法上,"{name}"映射到参数上,多个参数,以"/"隔开,放到"{}"中 List<Student> queryList(@PathParam("name")String name); } 4.创建SEI实现类 public class StudentInterfaceImpl implements StudentInterface { @Override public Student query(long id) { Student st = new Student(); st.setId(001); st.setName("张三"); st.setBirthday(new Date()); return st; } @Override public List<Student> queryList(String name) { Student st = new Student(); st.setId(001); st.setName("张三"); st.setBirthday(new Date()); Student st2 = new Student(); st2.setId(120); st2.setName("李四"); st2.setBirthday(new Date()); List<Student> list = new ArrayList<Student>(); list.add(st); list.add(st2); return list; } } 5.发布服务, JAXRSServerFactoryBean发布服务,3个参数,1:服务实现类;2.设置资源类;3.设置服务地址 public class StudentServer { public static void main(String[] args) { //JAXRSServerFactory发布REST的服务 JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean(); //设置服务实现类 jaxrsServerFactoryBean.setServiceBean(new StudentInterfaceImpl()); //设置资源类,如果有多个资源类,可以以","隔开 jaxrsServerFactoryBean.setResourceClasses(StudentInterfaceImpl.class); //设置服务地址 jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/user"); //发布服务 jaxrsServerFactoryBean.create(); } } 6.测试服务 http://127.0.0.1:12345/user/student/query/001 查询单个学生,返回XML数据 http://127.0.0.1:12345/user/student/queryList/001查询多个学生,返回JSON **注意:1)如果服务端发布时指定请求方式是GET(POST),客户端必须使用GET(POST)访问服务端,否则会报如下异常WARNING: javax.ws.rs.ClientErrorException: HTTP 405 Method Not Allowed 2)如果在同一方法上同时指定XML和JSON媒体类型,在GET请求下,默认返回XML,在POST请求下,默认返回JSON客户端: public class HttpClient { public static void main(String[] args) throws IOException { //第一步:创建服务地址,不是WSDL地址 URL url = new URL("http://127.0.0.1:12345/user/student/queryList/001"); //第二步:打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:设置参数 //3.1发送方式设置:POST\GET必须大写 connection.setRequestMethod("SET"); //3.2设置数据格式:content-type //3.3设置输入输出,因为默认新创建的connection没有读写权限, connection.setDoInput(true); //第五步:接收服务端响应,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } System.out.println(sb.toString()); is.close(); isr.close(); br.close(); } } }
5.CXF+Spring整合发布REST服务
1.创建web项目(引入jar包)2.创建POJO类3.创建SEI接口4.创建SEI实现类5.配置Spring配置文件,applicationContext.xml,<jaxrs:server,设置1.服务地址;2.服务实现类<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxrs:server>发布REST的服务 ,对JAXRSServerFactoryBean类封装,设置1.服务地址 2.服务实现类--> <jaxrs:server address="/user"> <jaxrs:serviceBeans> <ref bean="studentInterface"/> </jaxrs:serviceBeans> </jaxrs:server> <!-- 配置服务实现类 --> <bean name="studentInterface" class="cn.ws.rest.server.StudentInterfaceImpl"/></beans>6.配置web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置spring配置文件地址和加载的listener,配置CXF的servlet --> <!-- 设置spring的环境 --> <context-param> <!-- contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping></web-app>7.部署到tomcat下,启动tomcat8.测试服务 REST服务的使用说明书地址: http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user?_wadl客户端: 创建xxx.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function queryStudent(){ //创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); //打开连接 xhr.open("get","http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user/student/queryList/001?_type=json",true); //设置回调函数 xhr.onreadystatechange=function(){ //判断是否发送成功和判断服务端是否响应成功 if(4 == xhr.readyState && 200 == xhr.status){ alert(eval("("+xhr.responseText+")").student[0].name); } } //发送数据 xhr.send(null); } </script> </head> <body> <input type="button" value="查询" onclick="javascript:queryStudent();"/> </body> </html>
阅读全文
0 0
- WebService远程调用技术--学习02
- WebService远程调用技术--学习01
- WebService远程调用技术
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪之(Webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 远程调用技术代码追踪(webservice)
- 浅谈WebService跨平台远程调用技术
- webService远程调用技术二()
- 远程调用WebService
- WebService的远程调用
- 远程调用webservice
- c++远程调用webservice
- webservice远程调用详解
- axis2远程调用webservice
- 详解Java中的clone方法 -- 原型模式
- pat-1136. A Delayed Palindrome (20) 模拟
- 你有没有过奋不顾身的爱情,然后痛彻心扉的失去,你有过吗?
- 数组
- TensorFlow之基本结构
- WebService远程调用技术--学习02
- 图论——最短路
- SparkSQL的前世今生
- 分布式事务之说说TCC事务
- poj 3661 runnning 区间dp
- PAT 乙级 1076 WIFI密码
- CURL 访问https 发生 NSS error -12190 错误
- centos7安装subline
- Wannafly挑战赛5 B.可编程拖拉机比赛