安装cxf
环境变量配置:
JAVA_HOME,
CXF_HOME=cxf的目录
Path =%JAVA_HOME%\bin;%CXF_HOME%\bin;
CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar
使用wsdl2java生成客户端代码
wsdl2java -d . http://sdsdds/scd?wsdl
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://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"
serviceClass="cn.com.webxml.MobileCodeWSSoap">
</jaxws:client>
<!-- 配置本系统手机号查询service -->
<bean id="mobileService" class="cn.itcast.ws.mobile.service.MobileServiceImpl">
<!-- 注入查询公网手机号的客户端bean -->
<property name="mobileClient" ref="mobileClient"/>
</bean>
<!-- 发布手机号查询服务 -->
<jaxws:server address="/mobile" serviceClass="cn.itcast.ws.mobile.service.MobileService">
<jaxws:serviceBean>
<ref bean="mobileService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
springMVC.xml
<?xml version="1.0"?>
-<beans 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 " xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 组件扫描 只扫描action -->
<context:component-scan base-package="cn.itcast.ws.mobile.action"/>
<!-- 实现了处理器映射器和适配器 -->
<mvc:annotation-driven/>
<!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->
-<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的前缀 -->
<property value="/WEB-INF/jsp/" name="prefix"/>
<!-- 视图的后缀 -->
<property value=".jsp" name="suffix"/>
</bean>
</beans>
@WebService
public interface MobileService {
public String queryMobile(String code);
}
public class MobileServiceImpl implements MobileService {
//注入查询公网手机号的客户端bean(该bean在spring配置文件配置,通过spring注入)
private MobileCodeWSSoap mobileClient;
@Override
public String queryMobile(String code) {
//第一个参数:手机号至少 前7位
//第二个参数:用户id,非商业用户为空字符
//调用公网webservice
return mobileClient.getMobileCodeInfo(code, "");
}
public MobileCodeWSSoap getMobileClient() {
return mobileClient;
}
public void setMobileClient(MobileCodeWSSoap mobileClient) {
this.mobileClient = mobileClient;
}
}
@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.equals("")){
result = mobileService.queryMobile(code);
}
model.addAttribute("result", result);
//返回逻辑视图名
return "queryMobile";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
-<web-app version="2.5" id="WebApp_ID" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>ws_1231_cxf_spring_server</display-name>
<!-- 加载 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>
-<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>