diff --git a/mall-admin/src/main/java/com/macro/mall/component/RestAuthenticationEntryPoint.java b/mall-admin/src/main/java/com/macro/mall/component/RestAuthenticationEntryPoint.java index 5220b2f..6a107ed 100644 --- a/mall-admin/src/main/java/com/macro/mall/component/RestAuthenticationEntryPoint.java +++ b/mall-admin/src/main/java/com/macro/mall/component/RestAuthenticationEntryPoint.java @@ -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(); } } diff --git a/mall-admin/src/main/java/com/macro/mall/component/RestfulAccessDeniedHandler.java b/mall-admin/src/main/java/com/macro/mall/component/RestfulAccessDeniedHandler.java index 50d2171..8b22012 100644 --- a/mall-admin/src/main/java/com/macro/mall/component/RestfulAccessDeniedHandler.java +++ b/mall-admin/src/main/java/com/macro/mall/component/RestfulAccessDeniedHandler.java @@ -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(); } } diff --git a/mall-admin/src/main/java/com/macro/mall/component/WebLogAspect.java b/mall-admin/src/main/java/com/macro/mall/component/WebLogAspect.java index 50d3cb3..9cd396b 100644 --- a/mall-admin/src/main/java/com/macro/mall/component/WebLogAspect.java +++ b/mall-admin/src/main/java/com/macro/mall/component/WebLogAspect.java @@ -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; } diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/OssServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/OssServiceImpl.java index 713f6a7..8afc269 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/impl/OssServiceImpl.java +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/OssServiceImpl.java @@ -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); diff --git a/mall-admin/src/main/java/com/macro/mall/util/JsonUtil.java b/mall-admin/src/main/java/com/macro/mall/util/JsonUtil.java deleted file mode 100644 index 4008aab..0000000 --- a/mall-admin/src/main/java/com/macro/mall/util/JsonUtil.java +++ /dev/null @@ -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 jsonToPojo(String jsonData, Class beanType) { - try { - T t = MAPPER.readValue(jsonData, beanType); - return t; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - /** - * 将json数据转换成pojo对象list - */ - public static List jsonToList(String jsonData, Class beanType) { - JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); - try { - List list = MAPPER.readValue(jsonData, javaType); - return list; - } catch (Exception e) { - e.printStackTrace(); - } - - return null; - } - -} diff --git a/mall-admin/src/main/java/com/macro/mall/util/RequestUtil.java b/mall-admin/src/main/java/com/macro/mall/util/RequestUtil.java deleted file mode 100644 index 829e701..0000000 --- a/mall-admin/src/main/java/com/macro/mall/util/RequestUtil.java +++ /dev/null @@ -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,for支付宝异步回调,平时建议直接使用request.getParameterMap(),返回Map - */ - public static Map getParameterMap(HttpServletRequest request) { - Map result = new HashMap<>(); - Enumeration parameterNames = request.getParameterNames(); - while (parameterNames.hasMoreElements()) { - String parameterName = (String) parameterNames.nextElement(); - result.put(parameterName, request.getParameter(parameterName)); - } - return result; - } - -} diff --git a/mall-admin/src/test/com/macro/mall/PmsDaoTests.java b/mall-admin/src/test/com/macro/mall/PmsDaoTests.java index e464ad0..57db02c 100644 --- a/mall-admin/src/test/com/macro/mall/PmsDaoTests.java +++ b/mall-admin/src/test/com/macro/mall/PmsDaoTests.java @@ -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); } } diff --git a/mall-common/pom.xml b/mall-common/pom.xml index 0caee04..4fd6704 100644 --- a/mall-common/pom.xml +++ b/mall-common/pom.xml @@ -33,6 +33,12 @@ spring-data-commons 2.1.5.RELEASE + + + cn.hutool + hutool-all + 4.5.7 + \ No newline at end of file