Spring之整合Apache CXF框架实现WebServices远程调用

基于Spring实现WebSerivces远程调用.底层基于soap传输协议,wsdl对象描述规范

依赖的jar包, cxf版本:apache-cxf-2.7.17

这里写图片描述

1.配置: applicationContext.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"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- 导入CXF框架的spring 注入文件-->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

    <!-- 发布服务 -->
    <jaxws:endpoint implementor="com.ysdit.dtx.service.AdminInfoServiceImpl"
        address="/InfoPort" />

</beans>

2.创建接口:IAdminInfoService.java

import com.ysdit.dtx.entity.User;

import javax.jws.WebService;

@WebService
public interface IAdminInfoService {
    User getPwd();
}

3.创建实现类:AdminInfoServiceImpl.java

import com.ysdit.dtx.entity.User;

import javax.jws.WebService;

@WebService(endpointInterface = "com.ysdit.dtx.service.IAdminInfoService")
public class AdminInfoServiceImpl implements IAdminInfoService {
    @Override
    public User getPwd() {

        return new User();
    }
}

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>cxf-server</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
</web-app>

配置Tomcat或其它容器启动即可.我的访问路径:https://www.dtxzw.com/services/InfoPort?wsdl
这里写图片描述

看到这个,表示服务端配置成功.


但是有了这个,我们如何访问呢.我们可以利用cxf框架自带的命令给我生成能够解析这段xml语言的js文件,实现访问.

这里写图片描述

我们用到这个 wsdl2js

wsdl2js  http://localhost:8080/cxf-server/services/weatherPort?wsdl

会在当前目录生成js文件,才页面中引用这个文件即可

js调用页面:cxf_js.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'cxf_js.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script src="<%=basePath%>services/InfoPort?js"></script>
<script>
    function getInfo() {
        if(confirm('确定send吗?')){
            var wt = new service_dtx_ysdit_com__IAdminInfoService();
            wt.url = "<%=basePath%>services/InfoPort?wsdl";
            wt.getPwd(function(client) {
                alert(client._return._nickname);
            }, function(error) {
                alert(error);
            });
        }
    }
</script>
</head>

<body>
    <input type="button" value="send cxf request" onclick="getInfo()"  />
</body>
</html>

这里访问的是对象额.

这里写图片描述

完毕

百度云盘分享:http://pan.baidu.com/s/1i4Sr03N(cxf下载),http://pan.baidu.com/s/1jIjQuKA(示例下载)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一缕阳光直射你的心扉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值