parent
6851dc5ba2
commit
68c75b0650
@ -0,0 +1,61 @@
|
|||||||
|
package com.macro.mall.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jackson json序列化和反序列化工具类
|
||||||
|
*/
|
||||||
|
public class JsonUtil {
|
||||||
|
|
||||||
|
// 定义jackson对象
|
||||||
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将对象转换成json字符串。
|
||||||
|
*/
|
||||||
|
public static String objectToJson(Object data) {
|
||||||
|
try {
|
||||||
|
String string = MAPPER.writeValueAsString(data);
|
||||||
|
return string;
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将json结果集转化为对象
|
||||||
|
*
|
||||||
|
* @param jsonData json数据
|
||||||
|
* @param beanType 对象中的object类型
|
||||||
|
*/
|
||||||
|
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
|
||||||
|
try {
|
||||||
|
T t = MAPPER.readValue(jsonData, beanType);
|
||||||
|
return t;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将json数据转换成pojo对象list
|
||||||
|
*/
|
||||||
|
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) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.macro.mall.util;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* request工具类
|
||||||
|
*/
|
||||||
|
public class RequestUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除request指定参数
|
||||||
|
*/
|
||||||
|
public String removeParam(HttpServletRequest request, String paramName) {
|
||||||
|
String queryString = "";
|
||||||
|
Enumeration keys = request.getParameterNames();
|
||||||
|
while (keys.hasMoreElements()) {
|
||||||
|
String key = (String) keys.nextElement();
|
||||||
|
if (key.equals(paramName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ("".equals(queryString)) {
|
||||||
|
queryString = key + "=" + request.getParameter(key);
|
||||||
|
} else {
|
||||||
|
queryString += "&" + key + "=" + request.getParameter(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return queryString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取请求basePath
|
||||||
|
*/
|
||||||
|
public static String getBasePath(HttpServletRequest request) {
|
||||||
|
StringBuffer basePath = new StringBuffer();
|
||||||
|
String scheme = request.getScheme();
|
||||||
|
String domain = request.getServerName();
|
||||||
|
int port = request.getServerPort();
|
||||||
|
basePath.append(scheme);
|
||||||
|
basePath.append("://");
|
||||||
|
basePath.append(domain);
|
||||||
|
if("http".equalsIgnoreCase(scheme) && 80 != port) {
|
||||||
|
basePath.append(":").append(String.valueOf(port));
|
||||||
|
} else if("https".equalsIgnoreCase(scheme) && port != 443) {
|
||||||
|
basePath.append(":").append(String.valueOf(port));
|
||||||
|
}
|
||||||
|
return basePath.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求中参数转Map<String, String>,for支付宝异步回调,平时建议直接使用request.getParameterMap(),返回Map<String, String[]>
|
||||||
|
*/
|
||||||
|
public static Map<String, String> getParameterMap(HttpServletRequest request) {
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
Enumeration parameterNames = request.getParameterNames();
|
||||||
|
while (parameterNames.hasMoreElements()) {
|
||||||
|
String parameterName = (String) parameterNames.nextElement();
|
||||||
|
result.put(parameterName, request.getParameter(parameterName));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue