新增 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 jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -258,6 +258,7 @@ public class ServletUtils {
|
||||
if (object == null) {
|
||||
object = resultMap;
|
||||
}
|
||||
object = ResultUtils.result(object, request, response);
|
||||
if (jsonView != null) {
|
||||
return JsonMapper.toJson(object, jsonView);
|
||||
}else {
|
||||
@@ -331,6 +332,7 @@ public class ServletUtils {
|
||||
object = new JSONPObject(functionName, object);
|
||||
}
|
||||
}
|
||||
object = ResultUtils.result(object, request, response);
|
||||
if (jsonView != null) {
|
||||
return renderString(response, JsonMapper.toJson(object, jsonView));
|
||||
}else {
|
||||
@@ -358,18 +360,7 @@ public class ServletUtils {
|
||||
try {
|
||||
// response.reset(); // 注释掉,否则以前设置的Header会被清理掉,如ajax登录设置记住我的Cookie信息
|
||||
if (type == null && StringUtils.isBlank(response.getContentType())){
|
||||
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;
|
||||
}
|
||||
type = getContentType(string);
|
||||
}
|
||||
if (type != null) {
|
||||
response.setContentType(type);
|
||||
@@ -382,6 +373,27 @@ public class ServletUtils {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求的域名(含端口)
|
||||
*/
|
||||
|
||||
@@ -116,7 +116,7 @@ adminPath: /a
|
||||
# 前端基础路径
|
||||
frontPath: /f
|
||||
|
||||
# 加密设置
|
||||
# 加密设置(v5.8.1)
|
||||
encrypt:
|
||||
# 默认秘钥,可通过 AesUtils.genKeyString() 生成新秘钥 Hex 编码
|
||||
defaultKey: 9f58a20946b47e190003ec716c1c457d
|
||||
@@ -600,6 +600,14 @@ web:
|
||||
ajaxParamName: __ajax
|
||||
ajaxHeaderName: x-ajax
|
||||
|
||||
# 是否默认对结果进行统一包装为:{ code: 200, msg: "", data: {} | [] }(v5.8.1)
|
||||
# 注意:如果设置为 true 会对前端页面访问产生影响,暂时只为系统纯接口提供开启使用。
|
||||
isDefaultResult: false
|
||||
|
||||
# 开启对接口结果数据进行包装的请求参数名和请求头名(v5.8.1)
|
||||
resultParamName: __data
|
||||
resultHeaderName: x-header
|
||||
|
||||
# MVC 视图相关
|
||||
view:
|
||||
|
||||
|
||||
@@ -746,6 +746,14 @@ web:
|
||||
# # AJAX 接受参数名和请求头名(v4.3.0)
|
||||
# ajaxParamName: __ajax
|
||||
# ajaxHeaderName: x-ajax
|
||||
#
|
||||
# # 是否默认对结果进行统一包装为:{ code: 200, msg: "", data: {} | [] }(v5.8.1)
|
||||
# # 注意:如果设置为 true 会对前端页面访问产生影响,暂时只为系统纯接口提供开启使用。
|
||||
# isDefaultResult: false
|
||||
#
|
||||
# # 开启对接口结果数据进行包装的请求参数名和请求头名(v5.8.1)
|
||||
# resultParamName: __data
|
||||
# resultHeaderName: x-header
|
||||
#
|
||||
# # MVC 视图相关
|
||||
# view:
|
||||
|
||||
@@ -747,6 +747,14 @@ web:
|
||||
# ajaxParamName: __ajax
|
||||
# ajaxHeaderName: x-ajax
|
||||
#
|
||||
# # 是否默认对结果进行统一包装为:{ code: 200, msg: "", data: {} | [] }(v5.8.1)
|
||||
# # 注意:如果设置为 true 会对前端页面访问产生影响,暂时只为系统纯接口提供开启使用。
|
||||
# isDefaultResult: false
|
||||
#
|
||||
# # 开启对接口结果数据进行包装的请求参数名和请求头名(v5.8.1)
|
||||
# resultParamName: __data
|
||||
# resultHeaderName: x-header
|
||||
#
|
||||
# # MVC 视图相关
|
||||
# view:
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user