javabean转map 利用cglib的BeanMap类实现转换
import org.springframework.cglib.beans.BeanMap;
/**
*
* @Title: beanToMap
* @Description: javabean转map
* @param bean
* @return
*/
public static Map beanToMap(T bean) {
Map beanMap = new HashMap<>(16);
if (bean != null) {
BeanMap tempMap = BeanMap.create(bean);
for (Object key : tempMap.keySet()) {
String putKey = String.valueOf(key);
Object putValue = https://www.it610.com/article/tempMap.get(key);
beanMap.put(putKey, putValue);
}
}
return beanMap;
}/**
*
* @Title: beanListToMapList
* @Description: List转换为List
map转javabean 利用cglib的BeanMap类实现转换
import org.springframework.cglib.beans.BeanMap;
/**
*
* @Title: mapToBean
* @Description: map转javabean
* @param beanMap
* @param bean
* @return
*/
public static T mapToBean(Map beanMap, T bean) {
BeanMap tempMap = BeanMap.create(bean);
tempMap.putAll(beanMap);
return bean;
}/**
*
* @Title: mapToBean
* @Description: map转javabean
* @param beanMap
* @param beanClass
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static T mapToBean(Map beanMap, Class beanClass)throws InstantiationException, IllegalAccessException {
T bean = beanClass.newInstance();
BeanMap tempMap = BeanMap.create(bean);
tempMap.putAll(beanMap);
return bean;
}/**
*
* @Title: mapToBeanBatch
* @Description: List转List
* @param beanMap
* @param beanClass
* @return
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static List mapToBeanBatch(List beanMap, Class beanClass)
throws InstantiationException, IllegalAccessException {
List beanList = new ArrayList<>(16);
if (beanMap != null && beanMap.size() > 0) {
for (Map tempMap : beanMap) {
T bean = mapToBean(tempMap, beanClass);
beanList.add(bean);
}
}
return beanList;
}
xml转map 使用dom4j
/**
*
* @Title: parseXmlToMap
* @Description: 解析xml,并返回map
* @return
* @throws IOException
* @throws DocumentException
*/
public static Map parseXmlToMap(HttpServletRequest request) throws IOException, DocumentException {
// 将解析的结果存入map集合中
Map map = new HashMap<>(16);
// 从request中获取流
InputStream inputStream = request.getInputStream();
// 使用dom4j读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List elementList = root.elements();
// 迭代
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
inputStream.close();
inputStream = null;
return map;
}
xml转bean 【工具类|javabean、xml、json、map之间的相互转换】使用jackson
/**
*
* @Title: xmlToBean
* @Description: xml转换成bean
* @param request
* @param beanClass
* @return
* @throws IOException
*/
public static T xmlToBean(HttpServletRequest request, Class beanClass) throws IOException {
// 获取输入流
InputStream inputStream = request.getInputStream();
XmlMapper xmlMapper = new XmlMapper();
T bean = xmlMapper.readValue(inputStream, beanClass);
inputStream.close();
inputStream = null;
return bean;
}jackson相关注解
@JacksonXmlRootElement(localName = "xml")
public class TextMessage implements Serializable {
/**
* 序列化
*/
private static final long serialVersionUID = 2501424341457570501L;
/**
* 开发者微信号
*/
@JacksonXmlProperty(localName = "ToUserName")
@JacksonXmlCData(value = https://www.it610.com/article/true)
private String ToUserName;
....
}
推荐阅读
- 白皮书|阿拉丁指数丨《2021 年度小程序互联网发展白皮书》4.5 亿+DAU 小程序的 6 大发展趋势
- python|【python】微信朋友圈数据分析及可视化(爬虫+数据挖掘)
- 即便学会了Markdown,也没想到还可以这么干 —> 学会==提高效率
- XUtils3.0 框架(工具类)