15_CXF和Spring开发手机号查询网站

【整体分析】

 

【生成客户端代码】

wsdl网址: http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

生成的客户端代码

 

【工程截图(已拷入客户端生成代码)】

 

【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">

<!--    公网手机号查询客户端 
    正式开发时将address在单独的配置文件配置
    serviceClass:portType的类路径
    id:spring容器中bean的id
 -->
<jaxws:client id="mobileClient"
    address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
    serviceClass="cn.com.webxml.MobileCodeWSSoap">
</jaxws:client>


<!-- 配置本系统的手机号码查询service -->
<bean id="mobileService" class="cn.Higgin.ws.mobile.service.MobileServiceImpl">
    <!-- 注入查询公网手机号的客户端bean -->
    <property name="mobileClient" ref="mobileClient" />
</bean>

<!-- 发布手机号查询服务 -->
<jaxws:server address="/mobile" serviceClass="cn.Higgin.ws.mobile.service.MobileService">
    <jaxws:serviceBean>
        <ref bean="mobileService"/>
    </jaxws:serviceBean>
</jaxws:server>

</beans>    

关系:

注意:

其中的红框部分的address代码在 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL 找到:

而serviceClass则是在导入的客户端代码的MobileCodeWSSoap接口中:

 

 

【MobileService.java】(即SEI)

package cn.Higgin.ws.mobile.service;

import javax.jws.WebService;
/**SEI
 * 手机号查询的Service
 */
@WebService
public interface MobileService {
    
    public String queryMobile(String code);
    
}

【MobileServiceImpl.java】

package cn.Higgin.ws.mobile.service;

import cn.com.webxml.MobileCodeWSSoap;

/**
 *  SEI实现类
 */
public class MobileServiceImpl implements MobileService{

    //注入 查询公网手机号的客户端bean(该bean在spring配置文件配置,通过spring注入)
    private  MobileCodeWSSoap mobileClient;
    
    @Override
    public String queryMobile(String code) {
        /*
         *  调用公网的WebService
         * 第一个参数:手机号码至少前7位
         * 第二个参数:用户id,非商业用户为空字符串
         */
        return mobileClient.getMobileCodeInfo(code, ""); 
    }
    //get/Set方法
    public MobileCodeWSSoap getMobileClient() {
        return mobileClient;
    }

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

}

 

 【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>WebService_Spring_Mobile_Server</display-name>
  <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>
  
      <!-- 加载 spring容器 -->
     <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 本系统webservice的路径必须以/ws/开头 -->
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
    
    <!-- post乱码处理 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation指定 springmvc的全局配置文件 如果 contextConfigLocation不指定,默认找配置文件名称:servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
  
</web-app>

 

 【springmvc.xml】

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

    <!-- 组件扫描 只扫描action -->
    <context:component-scan base-package="cn.Higgin.ws.mobile.action" />

    <!-- 实现了处理器映射器和适配器 -->
    <mvc:annotation-driven />

    <!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 视图的后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
    
</beans>

 

 【MobileAction.java】

package cn.Higgin.ws.mobile.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.Higgin.ws.mobile.service.MobileService;

@Controller
public class MobileAction {
    
    @Autowired
    private MobileService mobileService;
    
    @RequestMapping("/queryMobile")
    public String queryMobile(Model model,String code) throws Exception{
        //调用service查询手机号
        String result=null;
        if(code!=null && !code.trim().equals("")){
            result =mobileService.queryMobile(code);
        }
        model.addAttribute("result",result);
        
        //返回逻辑视图名
        return "queryMobile";
    }
}

 

 【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="code" />
    <br/>
    查询结果:${result}
    <br/>
    <input type="submit" value="查询手机号归属地"/>
</form>
</body>
</html>

【运行结果】

输入任意正确格式的手机号码,即可查询出对应的查询结果。

 

posted @ 2016-09-06 15:50  HigginCui  阅读(655)  评论(0编辑  收藏  举报