博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring+CXF整合来管理webservice(服务器启动发布webservice)
阅读量:6632 次
发布时间:2019-06-25

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

Spring+CXF整合来管理webservice

    实现步骤:
      1. 添加cxf.jar 包(集成了Spring.jar、servlet.jar ),spring.jar包 ,servlet.jar 包
      2. 编写业务类,通过CXF来发布webservice
         员工管理:
       方法 :添加员工、查询员工  
            
      3. 添加一个CXF请求的 Servlet,用来处理webservice的请求
           过滤的地址/ws/*
      4. 配置Spring的配置文件: applicationContext.xml ,把cxf的bean在spring配置
          
      5. 在web.xml中配置 CXF的 Servlet , 添加spring的监听
      6. 通过wsimport生成本地代理 ,访问webservice

 

1.jar包:

 

 

2.业务类 

bean

package cn.it.ws.cxf.bean;public class Employee {    private Integer  id;    private String name;    private Integer age;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

 

接口

package cn.it.ws.cxf.b;import java.util.List;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import cn.it.ws.cxf.bean.Employee;@WebService(serviceName="EmployeeService")public interface EmployeeManager {    public abstract void add(@WebParam(name="employee")Employee employee);    public abstract @WebResult(name="employees")List
query();}

 

 实现类:

package cn.it.ws.cxf.b;import java.util.ArrayList;import java.util.List;import cn.it.ws.cxf.bean.Employee;/**员工管理的业务实现类 */public class EmployeeManagerImpl implements EmployeeManager {    private List
employees=new ArrayList<>(); @Override public void add(Employee employee){ //添加到集合中 employees.add(employee); } @Override public List
query(){ return employees; }}

 

 3.webxml配置CSFServlet(jar包自带的servlet)

cxf
org.apache.cxf.transport.servlet.CXFServlet
0
cxf
/ws/*

 

 4.配置spring的applicationContext.xml

(1)需要加入CXF约束:

 

 

jaxrs.xsd是restful的约束,restful与ws差不多,只是restful接收的是json数据。

 

记住命名空间路径

 

applicationContext.xml配置约束

增加约束先引入命名空间,再引入约束位置,因为有schemas目录,所以加上schemas

 

 

 

(2)applicationContext.xml配置

 

 5.web,xml配置spring监听

 

CXF_Server
cxf
org.apache.cxf.transport.servlet.CXFServlet
0
cxf
/ws/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml

 

 6.启动tomcat进行测试:

 

 

 

 

 7.生成本地代理

 

C:\Users\liqiang>cd DesktopC:\Users\liqiang\Desktop>wsimport http://localhost/CXF_server/ws/employeeManager?wsdlparsing WSDL...Generating code...Compiling code...

 

 

 打包:

C:\Users\liqiang\Desktop>jar -cvf EmployeeWS.jar ./cn已添加清单正在添加: cn/(输入 = 0) (输出 = 0)(存储了 0%)正在添加: cn/it/(输入 = 0) (输出 = 0)(存储了 0%)正在添加: cn/it/ws/(输入 = 0) (输出 = 0)(存储了 0%)正在添加: cn/it/ws/cxf/(输入 = 0) (输出 = 0)(存储了 0%)正在添加: cn/it/ws/cxf/b/(输入 = 0) (输出 = 0)(存储了 0%)正在添加: cn/it/ws/cxf/b/Add.class(输入 = 665) (输出 = 383)(压缩了 42%)正在添加: cn/it/ws/cxf/b/AddResponse.class(输入 = 432) (输出 = 286)(压缩了 33%)正在添加: cn/it/ws/cxf/b/Employee.class(输入 = 980) (输出 = 495)(压缩了 49%)正在添加: cn/it/ws/cxf/b/EmployeeManager.class(输入 = 1007) (输出 = 522)(压缩了48%)正在添加: cn/it/ws/cxf/b/EmployeeService.class(输入 = 2326) (输出 = 1049)(压缩了 54%)正在添加: cn/it/ws/cxf/b/ObjectFactory.class(输入 = 2506) (输出 = 902)(压缩了 64%)正在添加: cn/it/ws/cxf/b/package-info.class(输入 = 242) (输出 = 198)(压缩了 18%)正在添加: cn/it/ws/cxf/b/Query.class(输入 = 414) (输出 = 281)(压缩了 32%)正在添加: cn/it/ws/cxf/b/QueryResponse.class(输入 = 789) (输出 = 465)(压缩了 41%)

 

 

测试类:

import cn.it.ws.cxf.b.Employee;import cn.it.ws.cxf.b.EmployeeManager;import cn.it.ws.cxf.b.EmployeeService;public class _Main2 {    public static void main(String[] args) {        EmployeeService em = new EmployeeService();        EmployeeManager employeeManager = em.getEmployeeManagerPort();        Employee employee = new Employee();        employee.setAge(25);        employeeManager.add(employee);        System.out.println(employeeManager.query().get(0).getAge());    }}

 

客户端控制台:

25

 

服务端控制台:

九月 22, 2017 6:58:35 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log信息: Inbound Message----------------------------ID: 12Address: http://localhost/CXF_server/ws/employeeManager?wsdlHttp-Method: GETContent-Type: Headers: {Accept=[text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Type=[null], host=[localhost], user-agent=[Java/1.7.0_80]}--------------------------------------九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log信息: Inbound Message----------------------------ID: 13Address: http://localhost/CXF_server/ws/employeeManagerEncoding: UTF-8Http-Method: POSTContent-Type: text/xml; charset=utf-8Headers: {Accept=[text/xml, multipart/related], connection=[keep-alive], Content-Length=[204], content-type=[text/xml; charset=utf-8], host=[localhost], SOAPAction=[""], user-agent=[JAX-WS RI 2.2.4-b01]}Payload: 
25
--------------------------------------九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log信息: Outbound Message---------------------------ID: 13Encoding: UTF-8Content-Type: text/xmlHeaders: {}Payload:
--------------------------------------九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log信息: Inbound Message----------------------------ID: 14Address: http://localhost/CXF_server/ws/employeeManagerEncoding: UTF-8Http-Method: POSTContent-Type: text/xml; charset=utf-8Headers: {Accept=[text/xml, multipart/related], connection=[keep-alive], Content-Length=[163], content-type=[text/xml; charset=utf-8], host=[localhost], SOAPAction=[""], user-agent=[JAX-WS RI 2.2.4-b01]}Payload:
--------------------------------------九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log信息: Outbound Message---------------------------ID: 14Encoding: UTF-8Content-Type: text/xmlHeaders: {}Payload:
25
25
25
--------------------------------------

 

 

 

 

 

 

切换console:

 

转载地址:http://pcbvo.baihongyu.com/

你可能感兴趣的文章
WIN2008R2下安装plsqldeveloper和toad
查看>>
jquery 通过点击事件获取id
查看>>
Linux无人值守自动化安装详细配置流程!
查看>>
ibatis批量新增-自增长序列
查看>>
linux系统管理之九:rpm安装包
查看>>
Linux系统中查看日志的常用命令
查看>>
Linux下date命令使用举例说明
查看>>
AndroidManifest.xml文件解析
查看>>
【我的V日志】2010年1月29日星期五
查看>>
我的友情链接
查看>>
六种微服务架构的设计模式
查看>>
路由器配置大全
查看>>
备份单表数据
查看>>
Java豆瓣电影爬虫——抓取电影详情和电影短评数据
查看>>
ajax
查看>>
c语言插入排序
查看>>
052 自动将每日的日志增量导入到hive中
查看>>
Android NDK开发(五)--C代码回调Java代码【转】
查看>>
Linux systemd 打开调试终端、添加开机自运行程序
查看>>
教你一招:根据身份证号计算出生年月和年龄 text函数和mid函数混用 datedif函数和today函数混用...
查看>>