javabean转map
利用cglib的BeanMap类实现转换
import org.springframework.cglib.beans.BeanMap;
/**
*
* @Title: beanToMap
* @Description: javabean转map
* @param bean
* @return
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> beanMap = new HashMap<>(16);
if (bean != null) {
BeanMap tempMap = BeanMap.create(bean);
for (Object key : tempMap.keySet()) {
String putKey = String.valueOf(key);
Object putValue = tempMap.get(key);
beanMap.put(putKey, putValue);
}
}
return beanMap;
}
/**
*
* @Title: beanListToMapList
* @Description: List<T>转换为List<Map<String,Object>>
* @param beanList
* @return
public static <T> List<Map<String, Object>> beanToMapBatch(List<T> beanList) {
List<Map<String, Object>> beanMapList = new ArrayList<>(16);
if (beanList != null && beanList.size() > 0) {
for (T bean : beanList) {
Map<String, Object> beanMap = beanToMap(bean);
beanMapList.add(beanMap);
}
}
return
map转javabean
利用cglib的BeanMap类实现转换
import org.springframework.cglib.beans.BeanMap;
/**
*
* @Title: mapToBean
* @Description: map转javabean
* @param beanMap
* @param bean
* @return
public static <T> T mapToBean(Map<String, Object> 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
public static <T> T mapToBean(Map<String, Object> beanMap, Class<T> beanClass)throws InstantiationException, IllegalAccessException {
T bean = beanClass.newInstance();
BeanMap tempMap = BeanMap.create(bean);
tempMap.putAll(beanMap);
return bean;
}
/**
*
* @Title: mapToBeanBatch
* @Description: List<Map<String,Object>>转List<T>
* @param beanMap
* @param beanClass
* @return
* @throws IllegalAccessException
* @throws
public static <T> List<T> mapToBeanBatch(List<Map<String, Object>> beanMap, Class<T> beanClass)
throws InstantiationException, IllegalAccessException {
List<T> beanList = new ArrayList<>(16);
if (beanMap != null && beanMap.size() > 0) {
for (Map<String, Object> tempMap : beanMap) {
T bean = mapToBean(tempMap, beanClass);
beanList.add(bean);
}
}
return
xml转map
使用dom4j
/**
*
* @Title: parseXmlToMap
* @Description: 解析xml,并返回map
* @return
* @throws IOException
* @throws
public static Map<String, Object> parseXmlToMap(HttpServletRequest request) throws IOException, DocumentException {
// 将解析的结果存入map集合中
Map<String, Object> 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<Element> elementList = root.elements();
// 迭代
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
inputStream.close();
inputStream = null;
return
xml转bean
使用jackson
/**
*
* @Title: xmlToBean
* @Description: xml转换成bean
* @param request
* @param beanClass
* @return
* @throws
public static <T> T xmlToBean(HttpServletRequest request, Class<T> 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 = true)
private