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

本文介绍如何使用CXF和Spring框架整合发布Web服务,以查询手机归属地为案例,详细步骤包括项目创建、代码生成、SEI接口及其实现类创建、配置文件设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在以一个简单的案例手机归属地查询,模拟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服务,用于查询手机归属地案例完成,具体代码结构如下。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值