CXF+Spring整合发布web服务案例

现在以一个简单的案例手机归属地查询,模拟CXF+Spring整合发布web的服务。
一、服务端
基本步骤:
第一步:创建一个web项目,并且导入相关的jar包
在这里插入图片描述
第二步:生成公网客户端代码(具体代码结构如下)
在这里插入图片描述
第三步:创建SEI接口

package com.hhy.mobile.server;
/**
 * SEI接口
 * @Title:MobileInterface.java
 * @package:com.hhy.mobile.server
 * @date:2019-4-9
 */
public interface MobileInterface {
	public String queryMobile(String phoneNum);
}

第四步:创建SEI的实现类

package com.hhy.mobile.server;

import com.hhy.mobile.MobileCodeWSSoap;

/**
 * SEI接口的实现类
 * @Title:MobileInterfaceImpl.java
 * @package:com.hhy.mobile.server
 * @date:2019-4-9
 */
public class MobileInterfaceImpl implements MobileInterface {

	private MobileCodeWSSoap mobileClient;
	
	@Override
	public String queryMobile(String phoneNum) {
		return mobileClient.getMobileCodeInfo(phoneNum, "");
	}

	public MobileCodeWSSoap getMobileClient() {
		return mobileClient;
	}

	public void setMobileClient(MobileCodeWSSoap mobileClient) {
		this.mobileClient = mobileClient;
	}

}

第五步:创建queryMobile.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>手机号归属地查询</title>
</head>
<body>
	<form action="queryMobile.action" method="post">
		手机号归属地查询:<input type="text" name="phoneNum" /><input type="submit"
			value="查询" /><br />
		 查询结果: ${result}
	</form>
</body>
</html>

第六步:创建MobileServlet.java

package com.hhy.mobile.server.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hhy.mobile.server.MobileInterface;
/**
 * 
 * @Title:MobileServlet.java
 * @package:com.hhy.mobile.server.servlet
 * @author hehaiyang
 * @date:2019-4-9
 */
public class MobileServlet extends HttpServlet {
	private MobileInterface mobileServer;
	
	public void doGet(HttpServletRequest request,HttpServletResponse response){
		String phoneNum = request.getParameter("phoneNum");
		if (null != phoneNum && !"".equals(phoneNum)) {
			WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
			mobileServer= (MobileInterface) context.getBean("mobileServer");
			String result = mobileServer.queryMobile(phoneNum);
			request.setAttribute("result", result);
		}
		try {
			//记录一次bug:没有搞清文件路径,导致浏览器端报404
			request.getRequestDispatcher("/WEB-INF/jsp/queryMobile.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request,HttpServletResponse response){
		this.doGet(request, response);
	}
}

第七步:配置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">
	<!-- <jaxws:server发布服务-->
	<jaxws:server address="/mobile" serviceClass="com.hhy.mobile.server.MobileInterface">
		<jaxws:serviceBean>
			<ref bean="mobileServer"/>
		</jaxws:serviceBean>
	</jaxws:server>
	
	<!-- 配置服务的实现类 -->
	<bean name="mobileServer" class="com.hhy.mobile.server.MobileInterfaceImpl">
		<property name="mobileClient" ref="mobileClient"></property>
	</bean>
	
	<!-- 配置公网的客户端 -->
	<jaxws:client id="mobileClient" address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
				serviceClass="com.hhy.mobile.MobileCodeWSSoap">
	</jaxws:client>
</beans>

第八步:配置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"
	id="WebApp_ID" version="3.0">
	<display-name>hhy_ws_cxf_spring_rest_server</display-name>

	<!-- 设置spring的环境 -->
	<context-param>
		<!--contextConfigLocation是不能修改的 -->
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath: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>
	<!-- 配置mobileServlet -->
	<servlet>
		<servlet-name>mobileServlet</servlet-name>
		<servlet-class>com.hhy.mobile.server.servlet.MobileServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>mobileServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

第九步:部署到tomcat下,启动tomcat
在这里插入图片描述
第十步:测试
测试服务是否发布成功:在浏览器输入http://127.0.0.1:8080/hhy_ws_cxf_mobile/ws/mobile?wsdl,如果出现下面界面,表示服务发布成功。
在这里插入图片描述
测试查询界面,http://127.0.0.1:8080/hhy_ws_cxf_mobile/queryMobile.action:
在这里插入图片描述
至此,CXF+Spring整合发布web服务,用于查询手机归属地案例完成,具体代码结构如下。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值