1. 增加了libs子目录, 放置工具项目
2. MVC Starter 中增: 1) 加统一异常处理 2) 统一返回: BaseResponse 3) 框架异常类: BusinessException 4) 通用异常枚举: ResponseEnum
This commit is contained in:
@ -0,0 +1,66 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-common</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>mall-ebtp-cloud-common</name>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<resource.delimiter>@</resource.delimiter>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-parameter-names</artifactId>
|
||||
<version>2.12.0-rc1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jdk8</artifactId>
|
||||
<version>2.12.0-rc1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>2.12.0-rc1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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