Commit 44e2aa64 authored by 杜科's avatar 杜科

增加xml转换

parent 027bc448
...@@ -2,18 +2,19 @@ package com.yonde.common; ...@@ -2,18 +2,19 @@ package com.yonde.common;
import com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -25,9 +26,14 @@ public class JsonUtils { ...@@ -25,9 +26,14 @@ public class JsonUtils {
/** @deprecated */ /** @deprecated */
@Deprecated @Deprecated
private static ObjectMapper objectMapper = null; private static ObjectMapper objectMapper = null;
private static XmlMapper xmlMapper = null;
public JsonUtils() { public JsonUtils() {
} }
static {
xmlMapper = initXmlMapper();
}
private static ObjectMapper getObjectMapper() { private static ObjectMapper getObjectMapper() {
if (ObjectsUtil.isNull(objectMapper)) { if (ObjectsUtil.isNull(objectMapper)) {
...@@ -41,6 +47,69 @@ public class JsonUtils { ...@@ -41,6 +47,69 @@ public class JsonUtils {
return objectMapper; return objectMapper;
} }
private static XmlMapper initXmlMapper() {
XmlMapper newXmlMapper = new XmlMapper();
//newXmlMapper.findAndRegisterModules();
//newXmlMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
newXmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
newXmlMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
newXmlMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//newXmlMapper.setDefaultUseWrapper(false);
newXmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
return newXmlMapper;
}
/**
* 对象转为XML字符串
* @param value
* @return
* @throws JsonProcessingException
*/
public static String toXMLStr(Object value) throws JsonProcessingException {
return xmlMapper.writeValueAsString(value);
}
/**
* 对象转xml写入流
* @param value
* @return
* @throws IOException
*/
public static void writeXmlStr(OutputStream os, Object value) throws IOException {
xmlMapper.writeValue(os, value);
}
/**
* xml字符串转为对象
* @param <T>
* @param xmlString
* @param clazz
* @return
*/
public static <T> T fromXml(String xmlString, Class<T> clazz) {
if (StringUtils.isEmpty(xmlString)) {
return null;
}
try {
return xmlMapper.readValue(xmlString, clazz);
} catch (IOException e) {
log.error("parse xml string error:" + xmlString, e);
return null;
}
}
public static <T> T fromXml(InputStream xmlInput, Class<T> clazz) {
if (xmlInput == null) {
return null;
}
try {
return xmlMapper.readValue(xmlInput, clazz);
} catch (IOException e) {
log.error("parse xml input error", e);
return null;
}
}
public static String toJsonStr(Object value) { public static String toJsonStr(Object value) {
return toJsonStr(value, getObjectMapper()); return toJsonStr(value, getObjectMapper());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment