新增 web.isDefaultResult 参数,默认全局进行接口结果包装为 { code: 200, msg: "", data: {} | [] };新增 web.resultParamName 和 headerParamName 参数,对个别结果进行包装
This commit is contained in:
@@ -0,0 +1,80 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||||
|
* No deletion without permission, or be held responsible to law.
|
||||||
|
*/
|
||||||
|
package com.jeesite.common.web.http;
|
||||||
|
|
||||||
|
import com.jeesite.common.io.PropertiesUtils;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统一包装结果输出类:{ code: 200, msg: "", data: {} | [] }
|
||||||
|
* @author ThinkGem
|
||||||
|
* @version 2024-07-24
|
||||||
|
*/
|
||||||
|
public class ResultUtils {
|
||||||
|
|
||||||
|
private static final boolean isDefaultResult = PropertiesUtils.getInstance()
|
||||||
|
.getPropertyToBoolean("web.isDefaultResult", "false");
|
||||||
|
private static final String resultParamName = PropertiesUtils.getInstance()
|
||||||
|
.getProperty("web.resultParamName", "__data");
|
||||||
|
private static final String headerParamName = PropertiesUtils.getInstance()
|
||||||
|
.getProperty("web.headerParamName", "x-data");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置 web.isResult 参数可强制全局使用统一结果输出,否则,传递 __data=true 参数,或 x-data 请求头为 true 时启用
|
||||||
|
* @author ThinkGem
|
||||||
|
*/
|
||||||
|
public static Object result(Object data, HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
if (request != null && response != null && (isDefaultResult || (
|
||||||
|
"true".equals(request.getParameter(resultParamName))
|
||||||
|
|| "true".equals(request.getHeader(headerParamName))))) {
|
||||||
|
Object msg = request.getAttribute("message");
|
||||||
|
return new Result(response.getStatus(), msg, data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 结果对象
|
||||||
|
* @author ThinkGem
|
||||||
|
* @version 2024-07-24
|
||||||
|
*/
|
||||||
|
private static class Result {
|
||||||
|
private int code;
|
||||||
|
private String msg;
|
||||||
|
private Object data;
|
||||||
|
|
||||||
|
public Result(int code, Object msg, Object data) {
|
||||||
|
this.code = code;
|
||||||
|
this.msg = msg != null ? msg.toString() : null;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(int code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMsg(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(Object data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -12,14 +12,14 @@ import com.jeesite.common.lang.ExceptionUtils;
|
|||||||
import com.jeesite.common.lang.StringUtils;
|
import com.jeesite.common.lang.StringUtils;
|
||||||
import com.jeesite.common.mapper.JsonMapper;
|
import com.jeesite.common.mapper.JsonMapper;
|
||||||
import com.jeesite.common.mapper.XmlMapper;
|
import com.jeesite.common.mapper.XmlMapper;
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import org.apache.commons.lang3.Validate;
|
import org.apache.commons.lang3.Validate;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
@@ -258,6 +258,7 @@ public class ServletUtils {
|
|||||||
if (object == null) {
|
if (object == null) {
|
||||||
object = resultMap;
|
object = resultMap;
|
||||||
}
|
}
|
||||||
|
object = ResultUtils.result(object, request, response);
|
||||||
if (jsonView != null) {
|
if (jsonView != null) {
|
||||||
return JsonMapper.toJson(object, jsonView);
|
return JsonMapper.toJson(object, jsonView);
|
||||||
}else {
|
}else {
|
||||||
@@ -331,6 +332,7 @@ public class ServletUtils {
|
|||||||
object = new JSONPObject(functionName, object);
|
object = new JSONPObject(functionName, object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
object = ResultUtils.result(object, request, response);
|
||||||
if (jsonView != null) {
|
if (jsonView != null) {
|
||||||
return renderString(response, JsonMapper.toJson(object, jsonView));
|
return renderString(response, JsonMapper.toJson(object, jsonView));
|
||||||
}else {
|
}else {
|
||||||
@@ -358,18 +360,7 @@ public class ServletUtils {
|
|||||||
try {
|
try {
|
||||||
// response.reset(); // 注释掉,否则以前设置的Header会被清理掉,如ajax登录设置记住我的Cookie信息
|
// response.reset(); // 注释掉,否则以前设置的Header会被清理掉,如ajax登录设置记住我的Cookie信息
|
||||||
if (type == null && StringUtils.isBlank(response.getContentType())){
|
if (type == null && StringUtils.isBlank(response.getContentType())){
|
||||||
if ((StringUtils.startsWith(string, "{") && StringUtils.endsWith(string, "}"))
|
type = getContentType(string);
|
||||||
|| (StringUtils.startsWith(string, "[") && StringUtils.endsWith(string, "]"))){
|
|
||||||
type = MediaType.APPLICATION_JSON_VALUE;
|
|
||||||
}else if (StringUtils.startsWith(string, "<") && StringUtils.endsWith(string, ">")){
|
|
||||||
if (StringUtils.startsWith(string, "<!DOCTYPE")){
|
|
||||||
type = MediaType.TEXT_HTML_VALUE;
|
|
||||||
}else{
|
|
||||||
type = MediaType.APPLICATION_XML_VALUE;
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
type = MediaType.TEXT_PLAIN_VALUE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
response.setContentType(type);
|
response.setContentType(type);
|
||||||
@@ -382,6 +373,27 @@ public class ServletUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据内容判断相应类型 MediaType
|
||||||
|
* @return application/json、text/html、application/xml、text/plain
|
||||||
|
*/
|
||||||
|
public static String getContentType(String string) {
|
||||||
|
String type;
|
||||||
|
if ((StringUtils.startsWith(string, "{") && StringUtils.endsWith(string, "}"))
|
||||||
|
|| (StringUtils.startsWith(string, "[") && StringUtils.endsWith(string, "]"))){
|
||||||
|
type = MediaType.APPLICATION_JSON_VALUE;
|
||||||
|
}else if (StringUtils.startsWith(string, "<") && StringUtils.endsWith(string, ">")){
|
||||||
|
if (StringUtils.startsWith(string, "<!DOCTYPE")){
|
||||||
|
type = MediaType.TEXT_HTML_VALUE;
|
||||||
|
}else{
|
||||||
|
type = MediaType.APPLICATION_XML_VALUE;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
type = MediaType.TEXT_PLAIN_VALUE;
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取请求的域名(含端口)
|
* 获取请求的域名(含端口)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ adminPath: /a
|
|||||||
# 前端基础路径
|
# 前端基础路径
|
||||||
frontPath: /f
|
frontPath: /f
|
||||||
|
|
||||||
# 加密设置
|
# 加密设置(v5.8.1)
|
||||||
encrypt:
|
encrypt:
|
||||||
# 默认秘钥,可通过 AesUtils.genKeyString() 生成新秘钥 Hex 编码
|
# 默认秘钥,可通过 AesUtils.genKeyString() 生成新秘钥 Hex 编码
|
||||||
defaultKey: 9f58a20946b47e190003ec716c1c457d
|
defaultKey: 9f58a20946b47e190003ec716c1c457d
|
||||||
@@ -603,6 +603,14 @@ web:
|
|||||||
ajaxParamName: __ajax
|
ajaxParamName: __ajax
|
||||||
ajaxHeaderName: x-ajax
|
ajaxHeaderName: x-ajax
|
||||||
|
|
||||||
|
# 是否默认对结果进行统一包装为:{ code: 200, msg: "", data: {} | [] }(v5.8.1)
|
||||||
|
# 注意:如果设置为 true 会对前端页面访问产生影响,暂时只为系统纯接口提供开启使用。
|
||||||
|
isDefaultResult: false
|
||||||
|
|
||||||
|
# 开启对接口结果数据进行包装的请求参数名和请求头名(v5.8.1)
|
||||||
|
resultParamName: __data
|
||||||
|
resultHeaderName: x-header
|
||||||
|
|
||||||
# MVC 视图相关
|
# MVC 视图相关
|
||||||
view:
|
view:
|
||||||
|
|
||||||
|
|||||||
@@ -744,6 +744,14 @@ web:
|
|||||||
# ajaxParamName: __ajax
|
# ajaxParamName: __ajax
|
||||||
# ajaxHeaderName: x-ajax
|
# ajaxHeaderName: x-ajax
|
||||||
#
|
#
|
||||||
|
# # 是否默认对结果进行统一包装为:{ code: 200, msg: "", data: {} | [] }(v5.8.1)
|
||||||
|
# # 注意:如果设置为 true 会对前端页面访问产生影响,暂时只为系统纯接口提供开启使用。
|
||||||
|
# isDefaultResult: false
|
||||||
|
#
|
||||||
|
# # 开启对接口结果数据进行包装的请求参数名和请求头名(v5.8.1)
|
||||||
|
# resultParamName: __data
|
||||||
|
# resultHeaderName: x-header
|
||||||
|
#
|
||||||
# # MVC 视图相关
|
# # MVC 视图相关
|
||||||
# view:
|
# view:
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -744,6 +744,14 @@ web:
|
|||||||
# ajaxParamName: __ajax
|
# ajaxParamName: __ajax
|
||||||
# ajaxHeaderName: x-ajax
|
# ajaxHeaderName: x-ajax
|
||||||
#
|
#
|
||||||
|
# # 是否默认对结果进行统一包装为:{ code: 200, msg: "", data: {} | [] }(v5.8.1)
|
||||||
|
# # 注意:如果设置为 true 会对前端页面访问产生影响,暂时只为系统纯接口提供开启使用。
|
||||||
|
# isDefaultResult: false
|
||||||
|
#
|
||||||
|
# # 开启对接口结果数据进行包装的请求参数名和请求头名(v5.8.1)
|
||||||
|
# resultParamName: __data
|
||||||
|
# resultHeaderName: x-header
|
||||||
|
#
|
||||||
# # MVC 视图相关
|
# # MVC 视图相关
|
||||||
# view:
|
# view:
|
||||||
#
|
#
|
||||||
|
|||||||
Reference in New Issue
Block a user