调整common工程目录
This commit is contained in:
@ -0,0 +1,97 @@
|
||||
package com.chinaunicom.ebtp.mall.cloud.common.utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
||||
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* json util
|
||||
*
|
||||
* @author 付庆吉
|
||||
* @date 2020-09-16
|
||||
*/
|
||||
@Slf4j
|
||||
public class JsonUtils {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 将对象转换成json字符串。
|
||||
* <p>Title: pojoToJson</p>
|
||||
* <p>Description: </p>
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static String objectToJson(Object data) {
|
||||
try {
|
||||
MAPPER.registerModule(new ParameterNamesModule())
|
||||
.registerModule(new Jdk8Module())
|
||||
.registerModule(new JavaTimeModule());
|
||||
return MAPPER.writeValueAsString(data);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.info(ExceptionUtil.stacktraceToString(e));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json结果集转化为对象
|
||||
*
|
||||
* @param jsonData json数据
|
||||
* @param beanType 对象中的object类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
|
||||
try {
|
||||
return MAPPER.readValue(jsonData, beanType);
|
||||
} catch (Exception e) {
|
||||
log.info(ExceptionUtil.stacktraceToString(e));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将json数据转换成pojo对象list
|
||||
* <p>Title: jsonToList</p>
|
||||
* <p>Description: </p>
|
||||
*
|
||||
* @param jsonData
|
||||
* @param beanType
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
|
||||
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
|
||||
try {
|
||||
List<T> list = MAPPER.readValue(jsonData, javaType);
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
log.info(ExceptionUtil.stacktraceToString(e));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将List<DTO>转为List<VO>
|
||||
* <p>Title: jsonToList</p>
|
||||
* <p>Description: </p>
|
||||
*
|
||||
* @param jsonData
|
||||
* @param beanType
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> jsonToList(List jsonData, Class<T> beanType) {
|
||||
String str = objectToJson(jsonData);
|
||||
|
||||
return jsonToList(str,beanType);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package om.chinaunicom.ebtp.mall.cloud.common;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user