博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5、Web Service-整合CXF
阅读量:7098 次
发布时间:2019-06-28

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

 

 1、工程准备

 继续使用之前的服务端:https://www.cnblogs.com/Mrchengs/p/10562458.html

 

2、jar准备

 前去apache官网下载响应的jar:http://cxf.apache.org/download.html

 

3、在原来的工程中导入jar文件

 

其中提供的jar相对比较多可以根据开发需求去导入相应的jar!

 

启动服务:

可以看到使用的是jetty服务的

 

4、查看wsdl

 http://localhost:8081/webserviceserver/helloService?wsdl

 

 创建新的工程,将其拷贝到新的wsdl文件中进行编译

 

进行编译使用

环境变量的配置:https://www.cnblogs.com/ChrisMurphy/p/5224160.html

执行命令:

 

工程目录

 

 

 新建测试类:

 

5、测试类

package cn.com.client;import com.cr.service.HelloService;import com.cr.service.impl.HelloServiceImplService;public class client {     public static void main(String[] args){
HelloServiceImplService factory = new HelloServiceImplService(); HelloService hello = factory.getHelloServiceImplPort(); String res = hello.sayHello("mr"); System.out.println(res); }}

 

 

得到的结果如下:

 

6、分析请求和响应 

 

 request

br

 

 response

hello:br

 分析:

 

同时可以参考地址:https://www.cnblogs.com/yangh965/p/5046841.html

 

 

图解:

 

 

7、CXF支持的数据类型

1、基本数据类型:int、float、boolean等

2、引用类型

  -String

  -集合:数组、List、Set、Map

  -自定义类型

 

1、工程目录

 

此时仅仅添加了3个类:DateTypeWs.java  DateTypeWsImpl.java Student.java

 

2、类

Student.java

package com.cr.bean;public class Student {    private String name;    private int price;        //.....    }

 

DateTypeWs.java
package com.cr.service;import java.util.List;import java.util.Map;import javax.jws.WebMethod;import javax.jws.WebService;import com.cr.bean.Student;@WebServicepublic interface DateTypeWs {        @WebMethod    public boolean addStudent(Student stu);    @WebMethod    public Student gatStuByPrice(int price);    @WebMethod    public List
getStudentByprice(int price); @WebMethod public Map
getAllStu();}

 

DateTypeWsImpl.java 

package com.cr.service.impl;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.jws.WebService;import com.cr.bean.Student;import com.cr.service.DateTypeWs;@WebServicepublic class DateTypeWsImpl implements DateTypeWs {    @Override    public boolean addStudent(Student stu) {        // TODO Auto-generated method stub        System.out.println("addStudent:" + stu);        return true;    }    @Override    public Student gatStuByPrice(int price) {        System.out.println("gatStuByPrice:" + price);        return new Student("Mr",1000);    }    @Override    public List
getStudentByprice(int price) { System.out.println("getStudentByprice:" + price); List
list = new ArrayList
(); Student s1 = new Student("Mr1",1000); Student s2= new Student("Mr2",1000); list.add(s2); list.add(s1); return list; } @Override public Map
getAllStu() { System.out.println("getAllStu:"); Map
map = new HashMap
(); map.put(11, new Student("Mr11",1000)); map.put(22, new Student("Mr22",1000)); return map; }}

 

 启动服务:

package com.cr.server;import javax.xml.ws.Endpoint;import com.cr.service.impl.DateTypeWsImpl;import com.cr.service.impl.HelloServiceImpl;//发布servicepublic class server {          public static void main(String[] args){        String address = "http://localhost:8081/webserviceserver/helloService";        Endpoint.publish(address , new HelloServiceImpl());        String address1 ="http://localhost:8081/webserviceserver/dateTypeWs" ;        Endpoint.publish(address1  , new DateTypeWsImpl());        System.out.println("发布成功....");    }}

 

两个wsdl文件:

 3、测试端:

 将保存为:

 cmd:

 最终得到:

 

4、测试类:

package cn.com.client;import java.util.List;import service1.DateTypeWs;import service1.DateTypeWsImplService;import service1.Student;public class client2 {     public static void main(String[] args){                 DateTypeWsImplService factory = new DateTypeWsImplService();         DateTypeWs ws = factory.getDateTypeWsImplPort();                  Student stu = new Student();         stu.setName("mr");         stu.setPrice(100);         boolean sadd = ws.addStudent(stu);         System.out.println("addStudent(stu):" + sadd);                  Student stu1 =  ws.gatStuByPrice(1000);         System.out.println(stu1);                  List
list = ws.getStudentByprice(1000); System.out.println(list); System.out.println(ws.getAllStu()); }}

客户端的打印:

 

 服务端的打印

 

 

 此时可以看出关于CXF支持的数据类型

 这里只做了简单的测试

 其中Map的使用会有点问题(JDK不支持)

 

8、一次请求Web Service请求的流程

 一次请求web Service请求的本质:

(1)、浏览器向服务器发送一个soap消息(http请求+xml片段)

(2)、服务器端处理晚请求之后,向客户端返回一个soap消息

 流程:

 

转载于:https://www.cnblogs.com/Mrchengs/p/10569046.html

你可能感兴趣的文章
常用js函数整理--common.js
查看>>
java内存泄漏与内存溢出
查看>>
分布式与集群
查看>>
互联网服务器的实现过程需要考虑哪些安全问题 & 加解密及哈希知识点
查看>>
sql server2008给数据表,字段,添加修改注释
查看>>
meta标签清理缓存
查看>>
【数据结构】二叉树
查看>>
onvif开发之设备发现功能的实现--转
查看>>
虚拟机下linux迁移造成MAC地址异常处理办法
查看>>
数据库事务原子性、一致性是怎样实现的?[转]
查看>>
“营改增”后你该知道的…代开发票需要知道的16个事项
查看>>
arcgis10.1连接sqlserver数据库常见问题(转载)
查看>>
动态设置js的属性
查看>>
Fragment的setUserVisibleHint方法实现懒加载,但setUserVisibleHint 不起作用?
查看>>
@responsebody注解的作用就是让viewresolver不起作用,不返回视图名称而是直接返回的return object...
查看>>
lodash(二)对象+循环遍历+排序
查看>>
Eclipse快捷键大全
查看>>
Java -- 获取MAC地址
查看>>
Visual Prolog 的 Web 专家系统 (1)
查看>>
void 指针的转换
查看>>