# Controller

# 请求参数注解@QmBody

该注解为前端传递bodyjson数据时进行截获,并对类型进行自动转换。

注解和RequestBody的特性相似。

现在我们的接口是这样的是这样的。

@PostMapping("/demo2")
@ResponseBody
public String demo2 (@QmBody String testParams) {
    return QmResult.success(testParams);
}
1
2
3
4
5

前端传递参数示例

{
    "value":{
        "testParms":"内容"
    }
}
1
2
3
4
5

注解属性

/**
* 是否必须传递的参数
*/
boolean required() default true;

/**
* 当value的值或者参数名不匹配时,是否允许解析最外层属性到该对象
*/
boolean parseAllFields() default true;

/**
* 参数json中的别名key
*/
String value() default "";
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# QmResult

public static String success(String msg, Object data) {
    return QmResult.sendJson(QmCode._1, msg, data);
}

public static String fail(String msg, Object data) {
    return QmResult.sendJson(QmCode._2, msg, data);
}

public static String error(String msg, Object data) {
    return QmResult.sendJson(QmCode._500, msg, data);
}

public static String paramNull(String msg, Object data) {
    return QmResult.sendJson(QmCode._100, msg, data);
}

public static String paramFail(String msg, Object data) {
    return QmResult.sendJson(QmCode._101, msg, data);
}

public static String loginNotIn(String msg, Object data) {
    return QmResult.sendJson(QmCode._103, msg, data);
}

public static String permissionDenied(String msg, Object data) {
    return QmResult.sendJson(QmCode._104, msg, data);
}

public static String ssoError(String msg, Object data) {
    return QmResult.sendJson(QmCode._105, msg, data);
}

public static String unknowError(String msg, Object data) {
    return QmResult.sendJson(QmCode._999, msg, data);
}

//...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

上述为QmResult提供的静态方法,不再需要占用Controller的继承位置。

# 继承 QmController

注意:

从2.1.0开始继承QmController已成为过去式

现在使用的是更加优雅的静态工具QmResult进行数据返回封装,不再需要占用Controller的继承位置。

@Deprecated
public class QmController {
}
1
2
3

# 状态码枚举类源码

在调用QmResult时,类中已经定义好常用的状态类型,如果需要自定义,调用QmResult.sendJson(QmCode._xxx, msg, data);

QmCode枚举封装了我们规定的一套状态码规范。

具体源码如下:

public enum QmCode {
    /**
     * 成功
     */
    _1(1, "操作成功", "Success"),
    /**
     * 失败
     */
    _2(2, "操作失败", "Defeated"),
    /**
     * 参数提供不完整
     */
    _100(100, "参数提供不完整", "Parameter is incomplete"),
    /**
     * 参数错误
     */
    _101(101, "参数错误", "Parameter error"),
    /**
     * 版本号未通过
     */
    _102(102, "未通过版本校验", "Version validation failed"),
    /**
     * 未登录/登录过期/请求ip校验失败
     */
    _103(103, "登录状态已失效", "Login not in"),
    /**
     * 权限不足,拒绝访问
     */
    _104(104, "无访问权限", "Permission denied"),
    /**
     * 您的账号已在其他地方登录!
     */
    _105(105,"您的账号已在其他地方登录!","Your account has been logged in elsewhere"),
    /**
     * 请求404,找不到资源
     */
    _404(404, "资源丢失啦", "Can't find resources"),
    /**
     * 请求405,请求类型错误POST or GET
     */
    _405(405, "请求方式错误", "Wrong request mode"),
    /**
     * 请求415,不支持的媒体类型
     */
    _415(415, "不支持的媒体类型", "Unsupported media types"),
    /**
     * 服务器错误
     */
    _500(500, "服务器开小差啦", "Server Error"),
    /**
     * 未知错误
     */
    _999(999, "未知错误", "Server unknow");

    /**
     * 编码
     */
    private int code;
    /**
     * 中文msg
     */
    private String cnMsg;
    /**
     * 英文msg
     */
    private String enMsg;

    private QmCode(int code, String cnMsg, String enMsg) {
        this.code = code;
        this.cnMsg = cnMsg;
        this.enMsg = enMsg;
    }

    public int getCode() {
        return code;
    }

    /**
     * 根据code获取对应msg信息
     *
     * @param code
     * @return msg
     */
    public static final String getMsg(QmCode code) {
        return "cn".equalsIgnoreCase(TransmitConfiguration.responseMessageLang) ? code.cnMsg : code.enMsg;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
最近更新: 2021/10/12 下午5:44:25