Web service学习

          web service就是为了不同平调用台之间的调用,假如C写的程序需要由java调用。而不同平台之间调用需要某种媒介,使得能互相了解对方的意思。就像一个人会说中文,但不会说英文;另一个人会说英文,但不会说中文,这两个人要交谈,就需要第三个人,既会说中文,又会说英文来当两者的翻译。而XML就是不同语言之间的翻译,只要一种语言支持XML的生成与解析,那该语言就支持web service。下面简单运用一下。

       使用CXF开发web service。

        服务器端:

        1、要暴露的接口

package com.webservice.cxf;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
import com.webservice.cxf.util.XMLAdapter;

@WebService
public interface IHelloWorld
{
    //web service处理简单的字符串对象
    String sayHi(String name);

    //web service处理list集合
    List<Cat> getCatsByUser(User user);

    //web service处理其不支持的map集合,需要另加一个适配器,使其将不支持的类型转化为支持的类型
    @XmlJavaTypeAdapter(XMLAdapter.class) Map<String, Cat> getAllCats();
}
  2、实现类:

package com.webservice.cxf.impl;

import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.webservice.cxf.IHelloWorld;
import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
import com.webservice.cxf.service.IUserService;
import com.webservice.cxf.service.impl.UserServiceImpl;

@WebService(endpointInterface = "com.webservice.cxf.IHelloWorld", 
serviceName = "HelloWorldImpl")
//该serviceName对应的是客户端的实现接口的类名
public class HelloWorldImpl implements IHelloWorld
{

	@Override
	public String sayHi(String name)
	{
		return name + "您好";
	}

	@Override
	public List<Cat> getCatsByUser(User user)
	{
		//通过另外的service层实现具体的业务逻辑
		IUserService userService = new UserServiceImpl();
		return userService.getCatsByUser(user);
	}

	@Override
	public Map<String, Cat> getAllCats()
	{
		IUserService userService = new UserServiceImpl();
		return userService.getAllCats();
	}
}
3  javabean:
package com.webservice.cxf.domain;

public class Cat
{
	//一定要是包装类型,而不能是基本类型
	private Integer id;
	private String cat;
	private String color;

	public Cat(Integer id, String cat, String color)
	{
		super();
		this.id = id;
		this.cat = cat;
		this.color = color;
	}

	//一定要加上空构造
	public Cat()
	{
	}

	//set/get方法
}

package com.webservice.cxf.domain;

public class User
{
	//一定要是包装类型,而不能是基本类型
	private Integer id;
	private String name;
	private String pass;
	private String address;

	//一定要加上空构造
	public User()
	{
	}

	public User(Integer id, String name, String pass, String address)
	{
		super();
		this.id = id;
		this.name = name;
		this.pass = pass;
		this.address = address;
	}

	//set/get方法....
        //重写hashCode和equals,只要用户名和密码一致就认为是一个对象
	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((pass == null) ? 0 : pass.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (name == null)
		{
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (pass == null)
		{
			if (other.pass != null)
				return false;
		} else if (!pass.equals(other.pass))
			return false;
		return true;
	}
}

4   service接口:

package com.webservice.cxf.service;

import java.util.List;
import java.util.Map;

import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;

public interface IUserService
{
	List<Cat> getCatsByUser(User user);
	Map<String, Cat> getAllCats();
}

5、service接口的实现方法,具体的业务逻辑:

package com.webservice.cxf.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
import com.webservice.cxf.service.IUserService;

public class UserServiceImpl implements IUserService
{

	//通过map来模拟数据库里的信息
	static Map<User, List<Cat>> userDb = new HashMap<User, List<Cat>>();

	static
	{
		List<Cat> catList = new ArrayList<Cat>();
		catList.add(new Cat(1, "ketty", "白色"));
		catList.add(new Cat(2, "加菲", "黄色"));
		userDb.put(new User(1, "Tom", "1234", "tianjin"), catList);
		List<Cat> catList2 = new ArrayList<Cat>();
		catList2.add(new Cat(3, "机器", "蓝色"));
		catList2.add(new Cat(4, "哈哈", "红色"));
		userDb.put(new User(2, "Richard", "1234", "beijing"), catList2);
	}

	@Override
	public List<Cat> getCatsByUser(User user)
	{
		return userDb.get(user);
	}

	@Override
	public Map<String, Cat> getAllCats()
	{
		Map<String, Cat> catMap = new HashMap<String, Cat>();
		int i = 1;
		Collection<List<Cat>> catList = userDb.values();
		for (List<Cat> list : catList)
		{
			for (Cat cat : list)
			{
				catMap.put("第" + i++ + "个", cat);
			}
		}
		return catMap;
	}
}

6、由于webservice不能够处理返回map集合的类型,自定义适配器将webservice 不能处理的类型转化为它能够处理的类型:

package com.webservice.cxf.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.util.StringCat.Entity;

/**
 * 由于webservice不能够处理返回map集合的类型,自定义适配器将webservice 
 * 不能处理的类型转化为它能够处理的类型
 * XmlAdapter<StringCat, Map<String, Cat>> 后一个是不能处理的类型,前一个是可以处理的类型。
 */
public class XMLAdapter extends XmlAdapter<StringCat, Map<String, Cat>>
{

	//将map类型转化为一个对象类型,对象类型为webservice可以处理的类型
	@Override
	public StringCat marshal(Map<String, Cat> v) throws Exception
	{
		StringCat stringCat = new StringCat();
		for (String key : v.keySet())
		{
			stringCat.getEntities().add(new Entity(key, v.get(key)));
		}
		return stringCat;
	}

	//将一个对象类型转化为map类型
	@Override
	public Map<String, Cat> unmarshal(StringCat v) throws Exception
	{
		Map<String, Cat> result = new HashMap<String, Cat>();
		for (Entity entity : v.getEntities())
		{
			result.put(entity.getKey(), entity.getValue());
		}
		return result;
	}
}

class StringCat
{
	static class Entity
	{
		private String key;
		private Cat value;

		public Entity()
		{
		}

		public Entity(String key, Cat value)
		{
			this.key = key;
			this.value = value;
		}

		public String getKey()
		{
			return key;
		}

		public void setKey(String key)
		{
			this.key = key;
		}

		public Cat getValue()
		{
			return value;
		}

		public void setValue(Cat value)
		{
			this.value = value;
		}
	}

	private List<Entity> entities = new ArrayList<StringCat.Entity>();

	public List<Entity> getEntities()
	{
		return entities;
	}

	public void setEntities(List<Entity> entities)
	{
		this.entities = entities;
	}

	@Override
	public String toString()
	{
		return "StringCat [entities=" + entities + ", getEntities()="
				+ getEntities() + "]";
	}
}

7、服务器启动并暴露接口:


import com.webservice.cxf.impl.HelloWorldImpl;

public class TestMain
{

	public static void main(String[] args)
	{
		HelloWorldImpl helloWorldImpl = new HelloWorldImpl();
		Endpoint.publish("http://127.0.0.1/richard", helloWorldImpl);
		helloWorldImpl.sayHi("Richard");
		System.out.println("暴漏成功");
	}

}

 

客户端:在客户端只需要安装完毕CXF,并使用wsdl2java命令将服务器端的java或者class文件导入到客户端,客户端就可以像服务器端一样调用方法。客户端的方法调用:

package com.webservice.cxf.test;

import javax.xml.ws.Endpoint;
package com.webservice.cxf.test;

import java.util.List;

import com.webservice.cxf.Cat;
import com.webservice.cxf.Entity;
import com.webservice.cxf.IHelloWorld;
import com.webservice.cxf.StringCat;
import com.webservice.cxf.User;
import com.webservice.cxf.impl.HelloWorldImpl;

public class ClientTestmain
{

	public static void main(String[] args)
	{
		HelloWorldImpl factory = new HelloWorldImpl();
		IHelloWorld hw = factory.getHelloWorldImplPort();
		System.out.println(hw.sayHi("xushuai"));

		User user = new User();
		user.setId(1);
		user.setName("Richard");
		user.setPass("1234");
		List<Cat> catList = hw.getCatsByUser(user);
		for (Cat cat : catList)
		{
			System.out.println(cat.getCat());
		}

		StringCat stringCat = hw.getAllCats();
		List<Entity> ens = stringCat.getEntities();
		for (int i = 0; i < ens.size(); i++)
		{

			System.out.println(ens.get(i).getKey() + " | "
					+ ens.get(i).getValue().getCat());
		}
	}
}


}

在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值