请标准化json/xml等页面的Content-Type响应头,去掉AcceptCharset响应头 #I155OX

This commit is contained in:
thinkgem
2019-11-18 15:31:20 +08:00
parent 5e9c7a86e3
commit 68a304409c
3 changed files with 24 additions and 10 deletions

View File

@@ -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, "<!DOCTYPE")){
type = MediaType.TEXT_HTML_VALUE+";charset=UTF-8";
}else{
type = MediaType.APPLICATION_XML_VALUE;
}
}else{
type = "text/html";
type = MediaType.TEXT_PLAIN_VALUE;
}
}
response.setContentType(type);
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
} catch (IOException e) {
e.printStackTrace();

View File

@@ -39,5 +39,5 @@ cd ..
set "MAVEN_OPTS=%MAVEN_OPTS% -Xms256m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m"
call mvn clean test -Dmaven.test.skip=false -Dtest=com.jeesite.test.InitFilemanagerData,com.jeesite.test.InitCoreData -Djeesite.initdata=true -Djdbc.jta.enabled=false -U
msg 1 /time:0 /w "<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>..."
cmd /c msg %username% /time:0 /w "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>..."
pause

View File

@@ -11,8 +11,8 @@ echo.
%~d0
cd %~dp0
cd ..
call mvn clean package spring-boot:repackage -Dmaven.test.skip=true -U
rem cd ..
rem call mvn clean package spring-boot:repackage -Dmaven.test.skip=true -U
msg 1 /time:0 /w "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>..."
cmd /c msg %username% /time:0 /w "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>..."
pause