部分工具类实现改用hutool工具包

This commit is contained in:
macro 2019-04-29 20:31:52 +08:00
parent 0e347c3efe
commit ea0b1a0f82
8 changed files with 23 additions and 142 deletions

View File

@ -1,7 +1,7 @@
package com.macro.mall.component;
import cn.hutool.json.JSONUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.util.JsonUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@ -21,7 +21,7 @@ public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().println(JsonUtil.objectToJson(CommonResult.unauthorized(authException.getMessage())));
response.getWriter().println(JSONUtil.parse(CommonResult.unauthorized(authException.getMessage())));
response.getWriter().flush();
}
}

View File

@ -1,7 +1,7 @@
package com.macro.mall.component;
import cn.hutool.json.JSONUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.util.JsonUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
@ -23,7 +23,7 @@ public class RestfulAccessDeniedHandler implements AccessDeniedHandler{
AccessDeniedException e) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().println(JsonUtil.objectToJson(CommonResult.forbidden(e.getMessage())));
response.getWriter().println(JSONUtil.parse(CommonResult.forbidden(e.getMessage())));
response.getWriter().flush();
}
}

View File

@ -1,8 +1,10 @@
package com.macro.mall.component;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.macro.mall.bo.WebLog;
import com.macro.mall.util.JsonUtil;
import com.macro.mall.util.RequestUtil;
import io.swagger.annotations.ApiOperation;
import net.logstash.logback.marker.Markers;
import org.aspectj.lang.JoinPoint;
@ -68,7 +70,8 @@ public class WebLogAspect {
webLog.setDescription(log.value());
}
long endTime = System.currentTimeMillis();
webLog.setBasePath(RequestUtil.getBasePath(request));
String urlStr = request.getRequestURL().toString();
webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
webLog.setIp(request.getRemoteUser());
webLog.setMethod(request.getMethod());
webLog.setParameter(getParameter(method, joinPoint.getArgs()));
@ -83,8 +86,8 @@ public class WebLogAspect {
logMap.put("parameter",webLog.getParameter());
logMap.put("spendTime",webLog.getSpendTime());
logMap.put("description",webLog.getDescription());
// LOGGER.info("{}", JsonUtil.objectToJson(webLog));
LOGGER.info(Markers.appendEntries(logMap),JsonUtil.objectToJson(webLog));
// LOGGER.info("{}", JSONUtil.parse(webLog));
LOGGER.info(Markers.appendEntries(logMap), JSONUtil.parse(webLog).toString());
return result;
}

View File

@ -1,9 +1,11 @@
package com.macro.mall.service.impl;
import cn.hutool.json.JSONUtil;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.macro.mall.dto.OssCallbackParam;
import com.macro.mall.dto.OssCallbackResult;
import com.macro.mall.dto.OssPolicyResult;
import com.macro.mall.service.OssService;
@ -69,7 +71,7 @@ public class OssServiceImpl implements OssService {
byte[] binaryData = postPolicy.getBytes("utf-8");
String policy = BinaryUtil.toBase64String(binaryData);
String signature = ossClient.calculatePostSignature(postPolicy);
// String callbackData = BinaryUtil.toBase64String(JsonUtil.objectToJson(callback).getBytes("utf-8"));
// String callbackData = BinaryUtil.toBase64String(JSONUtil.parse(callback).toString().getBytes("utf-8"));
// 返回结果
result.setAccessKeyId(ossClient.getCredentialsProvider().getCredentials().getAccessKeyId());
result.setPolicy(policy);

View File

@ -1,62 +0,0 @@
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序列化和反序列化工具类
* Created by macro on 2018/4/26.
*/
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;
}
}

View File

@ -1,68 +0,0 @@
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工具类
* Created by macro on 2018/4/26.
*/
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;
}
}

View File

@ -1,11 +1,11 @@
package com.macro.mall;
import cn.hutool.json.JSONUtil;
import com.macro.mall.dao.PmsMemberPriceDao;
import com.macro.mall.dao.PmsProductDao;
import com.macro.mall.dto.PmsProductResult;
import com.macro.mall.model.PmsMemberPrice;
import com.macro.mall.util.JsonUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -48,7 +48,7 @@ public class PmsDaoTests {
@Test
public void testGetProductUpdateInfo(){
PmsProductResult productResult = productDao.getUpdateInfo(7L);
String json = JsonUtil.objectToJson(productResult);
String json = JSONUtil.parse(productResult).toString();
LOGGER.info(json);
}
}

View File

@ -33,6 +33,12 @@
<artifactId>spring-data-commons</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<!--Hutool Java工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.7</version>
</dependency>
</dependencies>
</project>