From 68a304409cc0e226d899b747b811bc66d2095249 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Mon, 18 Nov 2019 15:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=A0=87=E5=87=86=E5=8C=96json/xml?= =?UTF-8?q?=E7=AD=89=E9=A1=B5=E9=9D=A2=E7=9A=84Content-Type=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=A4=B4,=E5=8E=BB=E6=8E=89AcceptCharset=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=A4=B4=20#I155OX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeesite/common/web/http/ServletUtils.java | 26 ++++++++++++++----- web/bin/init-data.bat | 2 +- web/bin/package.bat | 6 ++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/web/http/ServletUtils.java b/common/src/main/java/com/jeesite/common/web/http/ServletUtils.java index c068fcb4..fb232faa 100644 --- a/common/src/main/java/com/jeesite/common/web/http/ServletUtils.java +++ b/common/src/main/java/com/jeesite/common/web/http/ServletUtils.java @@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.Validate; +import org.springframework.http.MediaType; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -100,7 +101,7 @@ public class ServletUtils { public static boolean isAjaxRequest(HttpServletRequest request){ String accept = request.getHeader("accept"); - if (accept != null && accept.indexOf("application/json") != -1){ + if (accept != null && accept.indexOf(MediaType.APPLICATION_JSON_VALUE) != -1){ return true; } @@ -189,13 +190,20 @@ public class ServletUtils { resultMap.put("data", data); } } + HttpServletResponse response = getResponse(); HttpServletRequest request = getRequest(); if (request != null){ String uri = request.getRequestURI(); if (StringUtils.endsWithIgnoreCase(uri, ".xml") || StringUtils .equalsIgnoreCase(request.getParameter("__ajax"), "xml")){ + if (response != null){ + response.setContentType(MediaType.APPLICATION_XML_VALUE); + } return XmlMapper.toXml(resultMap); }else{ + if (response != null){ + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + } String functionName = request.getParameter("__callback"); if (StringUtils.isNotBlank(functionName)){ return JsonMapper.toJsonp(functionName, resultMap); @@ -204,6 +212,9 @@ public class ServletUtils { } } }else{ + if (response != null){ + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + } return JsonMapper.toJson(resultMap); } } @@ -273,18 +284,21 @@ public class ServletUtils { public static String renderString(HttpServletResponse response, String string, String type) { try { // response.reset(); // 注释掉,否则以前设置的Header会被清理掉,如ajax登录设置记住我的Cookie信息 - if (type == null){ + if (type == null && StringUtils.isBlank(response.getContentType())){ if ((StringUtils.startsWith(string, "{") && StringUtils.endsWith(string, "}")) || (StringUtils.startsWith(string, "[") && StringUtils.endsWith(string, "]"))){ - type = "application/json"; + type = MediaType.APPLICATION_JSON_UTF8_VALUE; }else if (StringUtils.startsWith(string, "<") && StringUtils.endsWith(string, ">")){ - type = "application/xml"; + if (StringUtils.startsWith(string, "