All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.lshaci.framework.common.model.JsonResponse Maven / Gradle / Ivy

The newest version!
package top.lshaci.framework.common.model;

import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 

Web controller json response



* *

0.0.4:

*
 *     Add method: successMessage and message; Change the name of the params field to otherData
*
*

1.0.5:

*
 *     Move form framework web module to framework common module
 * 
*

1.0.9:

*
 *     1.使用继承hashmap的方式
 *     2.修改方法名
 * 
*

1.1.0:

*
 *     1.修改方法名为 1.0.5 版本
 *     2.Field: otherData 字段添加 final 修饰
 *     3.Method: otherData 方法入参修改为 public JsonResponse<R> otherData(Map<String, ? extends Object> otherData)
 * 
* * @author lshaci * @version 1.1.0 * @since 0.0.1 */ @Data @Accessors(chain = true) public class JsonResponse implements Serializable { private static final long serialVersionUID = 7574078101944305355L; /** * 默认失败响应码 */ private static final int default_failure_code = 500_0_00; /** * 默认失败响应消息 */ private static final String default_failure_message = "系统繁忙, 请稍后重试"; /** * 默认成功响应消息 */ private static final String default_success_message = "success"; /** * 状态信息; true: 成功, false: 失败 */ private boolean status; /** * 响应码 */ private int code; /** * 响应消息 */ private String message; /** * 需要返回的数据 */ private R data; /** * 返回添加的额外数据 */ @Getter private final Map otherData = new HashMap<>(); /** * Build a json response * * @param 响应数据类型 * @return json response */ public static JsonResponse build() { return new JsonResponse<>(); } /** * 失败响应 *
     * {
     *     "code":500,
     *     "status:false,
     *     "message":"未知异常, 请联系管理员"
     * }
     * 
* * @param 响应数据类型 * @return 响应 */ public static JsonResponse failure() { return failure(default_failure_message); } /** * 失败响应 *
     * {
     *     "code":500,
     *     "status:false,
     * }
     * 
* * @param message 失败消息 * @param 响应数据类型 * @return 响应 */ public static JsonResponse failure(String message) { return failure(default_failure_code, message); } /** * 失败响应 * * @param code 失败响应码 * @param message 失败消息 * @param 响应数据类型 * @return 响应 */ public static JsonResponse failure(int code, String message) { final JsonResponse jsonResponse = build(); jsonResponse.status = false; jsonResponse.code = code; jsonResponse.message = message; return jsonResponse; } /** * 成功响应 *
     * {
     *     "code":0,
     *     "status:true,
     *     "message":"success"
     * }
     * 
* * @param 响应数据类型 * @return 响应 */ public static JsonResponse success() { final JsonResponse jsonResponse = build(); jsonResponse.status = true; jsonResponse.message = default_success_message; return jsonResponse; } /** * 成功响应数据 * * @param data 数据 * @param 响应数据类型 * @return 响应 */ public static JsonResponse success(R data) { final JsonResponse jsonResponse = success(); jsonResponse.data = data; return jsonResponse; } /** * 成功响应消息 * * @param message 消息 * @param 响应数据类型 * @return 响应 */ public static JsonResponse successMessage(String message) { final JsonResponse jsonResponse = success(); jsonResponse.message = message; return jsonResponse; } /** * 响应数据 * * @param data 数据 * @return 响应 */ public JsonResponse data(R data) { this.data = data; return this; } /** * 响应其它数据 * * @param otherData 其它数据 * @return 响应 */ public JsonResponse otherData(Map otherData) { this.otherData.putAll(otherData); return this; } /** * 响应数据 * * @param key 数据键 * @param value 数据值 * @return 响应 */ public JsonResponse otherData(String key, Object value) { this.otherData.put(key, value); return this; } /** * 响应消息 * * @param message 消息 * @return 响应 */ public JsonResponse message(String message) { this.message = message; return this; } /** * 响应码 * * @param code 响应码 * @return 响应 */ public JsonResponse code(int code) { this.code = code; return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy