webservice--综合案例

综合案例

1、需求

l  集成公网手机号归属地查询服务

l  对外发布自己的手机号归属地查询服务

l  提供查询界面

2、分析

3、实现

开发步骤:

第一步:创建web项目(引入jar包)

第二步:生成公网客户端代码

第三步:创建SEI接口

import javax.jws.WebService;

/**
 * 
 * <p>Title: MobileInterface.java</p>
 * <p>Description:SEI接口</p>*/
@WebService
public interface MobileInterface {

	public String queryMobile(String phoneNum);
}
 

第四步:创建SEI实现类

package cn.hcx.mobile.server;

import cn.hcx.mobile.MobileCodeWSSoap;

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 cn.hcx.mobile.server.servlet;

import java.io.IOException;

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

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

import cn.hcx.mobile.server.MobileInterface;

/**
 * 
 * <p>
 * Title: MobileServlet.java
 * </p>
 * <p>
 * Description:MobileServlet
*/
public class MobileServlet extends HttpServlet {
	
	private MobileInterface mobileServer;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String phoneNum = request.getParameter("phoneNum");
		if(null != phoneNum && !"".equals(phoneNum)){
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
			mobileServer = (MobileInterface) context.getBean("mobileServer");
			String result = mobileServer.queryMobile(phoneNum);
			request.setAttribute("result", result);
		}
		request.getRequestDispatcher("/WEB-INF/jsp/queryMobile.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
 

第七步:配置spring配置文件,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"
	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="cn.hcx.mobile.server.MobileInterface">
		<jaxws:serviceBean>
			<ref bean="mobileServer"/>
		</jaxws:serviceBean>
	</jaxws:server>
	<!-- 配置服务实现类 -->
	<bean name="mobileServer" class="cn.hcx.mobile.server.MobileInterfaceImpl">
		<property name="mobileClient" ref="mobileClient"/>
	</bean>
	
	<!-- 配置公网客户端 -->
	<jaxws:client id="mobileClient" address="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" 
		serviceClass="cn.hcx.mobile.MobileCodeWSSoap"/>
	
</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>ws_2_cxf_spring_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>cn.hcx.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.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 

 

第九步:部署到tomcat下,启动tomcat

第十步:测试

         测试服务是否发布成功

         测试查询界面


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值