便民网站上的天气预报 手机归属地查询等 是如何实现的呢?
实质上这些信息我们的数据库是不存在的 只能通过远程调用来获取,要开发这样的功能,需要解决以下方案:
(1)Socket(服务端:开启服务 客户端:连接服务)
(2)通过http调用实现(客户端通过HttpClient调用)
(3)Webservice
(4)RMI(远程方法调用)java 调用java C# remoting
cxf框架:
例子:
服务端:
接口:
@WebService //注解代表它是一个webservice服务 public interface IWeatherService { public String getWeatherByCityName(String cityName); }
实现类:
public class WeatherServiceImpl implements IWeatherService { public String getWeatherByCityName(String cityName) { if("北京".equals(cityName)){ return "雾霾"; }else if("深圳".equals(cityName)){ return "晴"; }else if("东莞".equals(cityName)){ return "阴"; }else { return "其它"; } } }
测试类:
注意:服务端的测试不要使用junit。
public class PublisherWebServiceTest { //引入cxf的api实现webservice服务的发布 public static void main(String[] args) { //1.创建一个服务的工厂类对象 JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); //2.设置服务的访问地址 factory.setAddress("http://localhost:12345/weather"); //3.设置服务的真正的实现类的对象 factory.setServiceBean(new WeatherServiceImpl()); //4.服务webservice服务 factory.create(); } }
说明书(从下往上读 下面图片已经调整 正常阅读即可)-- ?wsdl
客户端:
命令行进入要拷贝的目标文件夹 有时候有问题 可以直接在目标文件夹下shift+右键进入
直接运行 显示缺失URI
指令:
测试类:
public class ClientTest { @Test public void testWebservice(){ //1.得到proxy代理对象 JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean(); //2.指定代理的访问服务端的地址 proxy.setAddress("http://localhost:12345/weather?wsdl"); //3.设置代理对那个接口进行代理 proxy.setServiceClass(com.zixue.webservice.IWeatherService.class); //4.生成上面IWeatherService接口的代理子类对象 IWeatherService iws=(IWeatherService)proxy.create(); //5.调用方法(远程方法调用) String result = iws.getWeatherByCityName("深圳"); System.out.println(result); } }
CXF与Spring整合:
开发步骤:
服务端
(1)创建web工程 添加jar包
(2)修改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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>sz06ws01</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>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--CXF框架的核心控制器的加载 --> <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> </web-app>
(3)添加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:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd "> <!--配置服务端 --> <!--生成服务的实现类的对象 --> <bean id="WeatherServiceImpl" class="com.zixue.webservice.WeatherServiceImpl"></bean> <!--实现天气服务的发布 地址以及注入实现类对象 --> <jaxws:server address="/weather"> <jaxws:serviceBean> <ref bean="WeatherServiceImpl"></ref> </jaxws:serviceBean> </jaxws:server> </beans>
(4)编写服务类
接口和实现类
(5)测试服务端
客户端
(1)创建web工程 添加jar包
(2)wsimport 生成本地代码
(3)编写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:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd "> <!-- 客户端--> <jaxws:client id="weatherClient" address="http://localhost:8080/sz06ws01/ws/weather?wsdl" serviceClass="com.zixue.webservice.IWeatherService " ></jaxws:client> </beans>
(4)测试类
public class ClientTest { @Test public void testWs(){ ApplicationContext ac =new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); IWeatherService iws = (IWeatherService) ac.getBean("weatherClient"); String result = iws.getWeatherByCityName("深圳"); System.out.println(result); } }