完善修正xml转json消息的问题。
This commit is contained in:
+5
-12
@@ -13,25 +13,18 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
|
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
|
||||||
|
import com.gzzn.omms.msgexchangeapi.entity.msg.MSG;
|
||||||
import com.gzzn.omms.msgexchangeapi.exception.ExchangeServiceException;
|
import com.gzzn.omms.msgexchangeapi.exception.ExchangeServiceException;
|
||||||
|
import com.gzzn.omms.msgexchangeapi.utils.JsonUtil;
|
||||||
|
|
||||||
@Service("exchangeService")
|
@Service("exchangeService")
|
||||||
public class ExchangeServiceImpl implements IExchangeService {
|
public class ExchangeServiceImpl implements IExchangeService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String xmlstrToJson(String xml) throws ExchangeServiceException {
|
public String xmlstrToJson(String xml) throws ExchangeServiceException {
|
||||||
try {
|
MSG msg = xmlstrToObject(xml,MSG.class); //转具体消息
|
||||||
Map map;
|
String jsonStr = JsonUtil.getStringWithNoneKey(msg); //转json同时去除null的key
|
||||||
ObjectMapper xmlMapper = new XmlMapper();
|
return jsonStr;
|
||||||
map = xmlMapper.readValue(xml, Map.class);
|
|
||||||
map.remove("noNamespaceSchemaLocation");
|
|
||||||
|
|
||||||
ObjectMapper jsonMapper = new ObjectMapper();
|
|
||||||
String json = jsonMapper.writer().withDefaultPrettyPrinter().writeValueAsString(map);
|
|
||||||
return json;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new ExchangeServiceException(e.getMessage(),e);
|
|
||||||
}
|
|
||||||
} //end function
|
} //end function
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.gzzn.omms.msgexchangeapi.utils;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
@@ -22,6 +23,26 @@ public class JsonUtil {
|
|||||||
}
|
}
|
||||||
} //end function
|
} //end function
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取json字符串去除值为null的key
|
||||||
|
* @param object
|
||||||
|
* @return json字符串
|
||||||
|
*/
|
||||||
|
public static String getStringWithNoneKey(Object object)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
|
|
||||||
|
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
|
||||||
|
java.lang.String jsonStr;
|
||||||
|
jsonStr = ow.writeValueAsString(object);
|
||||||
|
return jsonStr;
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} //end function
|
||||||
|
|
||||||
|
|
||||||
public static <T> T getObject(String str,Class<T> valueType) throws JsonParseException, JsonMappingException, IOException
|
public static <T> T getObject(String str,Class<T> valueType) throws JsonParseException, JsonMappingException, IOException
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user