一.WebService数据传输的方式
1.SOAP:http+xml
2.Http rest :一套用来创建Web Service的方法,REST式的Web Service使用HTTP里的方法:GET,POST,DELETE, PUT
3.http自定义数据协议:比如http传输json数据,http传输xml数据等
二.CXF概述
1.CXF是Apache旗下的,所以全称叫Apache CXF
2.Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。
3.Apache CXF 是一个开源的web Services 框架CXF 帮助您构建和开发 web Services 它支持多种协议,比如:SOAP1.1,1,2、XML/HTTP、RESTful HTTP 或者 CORBA。
4.CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)
5.Cxf是基于SOA总线结构,依靠spring完成模块的集成,实现SOA方式。
6.CXF可以灵活的部署:可以运行有Tomcat,Jboss,Jetty(内置),weblogic上面。
三.CXF开发webService
1.http://cxf.apache.org/download.html 下载CXF 解压到电脑(/opt/apache-cxf-3.3.0)
2.配置环境变量
sudo vim /opt/profile 增加如下内容
# set CXF environment
export CXF_HOME=/opt/apache-cxf-3.3.0
export PATH=$PATH:$CXF_HOME/bin
3.导包
将下载的apache-cxf-3.3.0解压,把目录下lib所有的jar包复制到项目中
(1)发布服务
编写接口 WeatherInterface
package com.gyf.weather;
import javax.jws.WebService;
// 添加此注解标记这个接口是webService
@WebService
public interface WeatherInterface {
/**
* 根据城市获取天气信息
* @param cityName
* @return
*/
public String quertWeather(String cityName);
/**
*
* 根据省份 获取城市
* @param provinceName
* @return
*/
public String[] getCityNameByProvince(String provinceName);
}
编写实现类WeatherInterfaceImpl
package com.gyf.weather.impl;
import com.gyf.weather.WeatherInterface;
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String quertWeather(String cityName) {
if ("广州".equals(cityName)){
return cityName + ":天气晴,白天最高温度30度,未来三天都晴";
}else {
return cityName + ":天气雨,白天最高温度30度,未来三天都雨";
}
}
@Override
public String[] getCityNameByProvince(String provinceName) {
if ("广东".equals(provinceName)){
return new String[]{"广州", "深圳", "珠海", "东莞"};
}else {
return new String[]{"未开通"};
}
}
}
编写main方法发布接口
import com.gyf.weather.impl.WeatherInterfaceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Main {
public static void main(String[] args) {
// 发布一个webservice服务
// 创建服务工厂Bean
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
// 设置服务实现对象
factoryBean.setServiceBean(new WeatherInterfaceImpl());
// 设置服务发布地址
factoryBean.setAddress("http://localhost:5678/weather");
// 创建服务
factoryBean.create();
}
}
(2)编写客户端
导包(同服务端一样)
创建java项目
终端进入项目src目录下,执行命令生成客户端代码
wsdl2java -d . http://localhost:5678/weather?wsdl
其中
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
编写Main方法
import com.gyf.weather.WeatherInterface;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 使用CXF方式调用
// 1.创建bean
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
// 2.设置接口
factoryBean.setServiceClass(WeatherInterface.class);
// 3.设置地址
factoryBean.setAddress("http://localhost:5678/weather?wsdl");
// 4.创建服务
WeatherInterface weatherInterface = factoryBean.create(WeatherInterface.class);
List<String> cities = weatherInterface.getCityNameByProvince("广东");
System.out.println(cities);
String weather = weatherInterface.quertWeather("深圳");
System.out.println(weather);
}
}
输出结果:
[广州, 深圳, 珠海, 东莞]
深圳:天气雨,白天最高温度30度,未来三天都雨
四.CXF+Spring整合发布SOAP服务(基于weblogic服务端)
1.创建web项目
2.导入包
详见/home/miracle/Desktop/java
3.编写接口 WeatherService
package com.gyf.service;
import javax.jws.WebService;
// 告诉spring这个接口是webService接口
@WebService
public interface WeatherService {
/**
* 根据城市获取天气信息
* @param cityName
* @return
*/
public String quertWeather(String cityName);
/**
*
* 根据省份 获取城市
* @param provinceName
* @return
*/
public String[] getCityNameByProvince(String provinceName);
}
4.编写实现类 WeatherServiceImpl
package com.gyf.service.impl;
import com.gyf.service.WeatherService;
public class WeatherServiceImpl implements WeatherService {
@Override
public String quertWeather(String cityName) {
if ("广州".equals(cityName)){
return cityName + ":天气晴,白天最高温度30度,未来三天都晴";
}else {
return cityName + ":天气雨,白天最高温度30度,未来三天都雨";
}
}
@Override
public String[] getCityNameByProvince(String provinceName) {
if ("广东".equals(provinceName)){
return new String[]{"广州", "深圳", "珠海", "东莞"};
}else {
return new String[]{"未开通"};
}
}
}
5.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: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/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 通过spring发布webservice服务 -->
<!-- 1.创建 天气服务对象 -->
<bean id="weatherService" class="com.gyf.service.impl.WeatherServiceImpl"></bean>
<!-- 2.发布服务 -->
<!-- 方式一 -->
<!-- <jaxws:server address="/weather" serviceClass="com.gyf.service.WeatherService">
<jaxws:serviceBean>
<ref bean="weatherService"></ref>
</jaxws:serviceBean>
</jaxws:server>-->
<!-- 方式二 -->
<jaxws:endpoint address="/weather" implementor="com.gyf.service.impl.WeatherServiceImpl"></jaxws:endpoint>
</beans>
6.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 1.加载spring配置文件 -->
<context-param>
<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>
<!-- 2.配置cxf servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!-- 3.配置cxf的servlet映射路径(访问项目/ws/下的路径 会被cxf拦截到) -->
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
五.CXF+Spring整合SOAP客户端(基于weblogic客户端)
1.创建一个javaweb项目(同上)
2.导入cxf的jar包(同上)
3.生成客户端面代码
cd src
wsdl2java -d . http://localhost:7001/web_cxf_spring/ws/weather?wsdl
4.配置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: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不需要写 ?wsdl -->
<jaxws:client id="weatherService" address="http://localhost:7001/web_cxf_spring/ws/weather" serviceClass="com.gyf.service.WeatherService">
</jaxws:client>
</beans>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 1.加载spring配置文件 -->
<context-param>
<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>
</web-app>
6.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>天气查询</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/WeatherSearchServlet" method="post">
城市:<input type="text" name="cityName"><input type="submit">
</form>
天气信息:<br>
${weatherInfo}
</body>
</html>
7.Servlet
package com.gyf.web;
import com.gyf.service.WeatherService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/WeatherSearchServlet")
public class WeatherSearchServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决post请求乱码
request.setCharacterEncoding("UTF-8");
//1.获取参数
String cityName = request.getParameter("cityName");
//2.获取weatherService,【在web.xml中加载spring配置文件】
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
WeatherService weatherService = (WeatherService) context.getBean("weatherService");
//3.调用weatherService
String weatherInfo = weatherService.quertWeather(cityName);
//4.把数据存在request
request.setAttribute("weatherInfo",weatherInfo);
//5.返回一个jsp页面,在jsp页面显示数据
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
8.效果