# Controller
# 请求参数注解@QmBody
该注解为前端传递
body
的json
数据时进行截获,并对类型进行自动转换。
注解和
RequestBody
的特性相似。
现在我们的Controller
是这样的。
@PostMapping("/demo2")
public String demo2 (@QmBody String testParams) {
return super.sendJSON(QmCode._1,testParams);
}
1
2
3
4
2
3
4
前端传递参数示例
{
"value":{
"testParms":"内容"
}
}
1
2
3
4
5
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
2
3
4
5
6
7
8
9
10
11
12
13
14
# 继承QmController
一般我们返回给前端数据时,需要固定的
json
格式。而这些QmController
已经帮助实现了。
该类提供了以下共同使用的属性和方法
- sendJSON(.....);
- 该方法提供了三个重载方法,方便调用。
源码是这样的
public class QmController {
/**
* 接口回调方法
*
* @param code QmCode
* @return
*/
public String sendJSON(QmCode code) {
Map<String, Object> responseMap = new HashMap<>(16);
responseMap.put("code", code.getCode());
responseMap.put("msg", QmCode.getMsg(code));
responseMap.put("data", null);
responseMap.put("responseTime", new Date());
return this.parseJsonToResponse(responseMap);
}
/**
* 接口回调方法
*
* @param code QmCode
* @param data 传递数据
* @return
*/
public String sendJSON(QmCode code, Object data) {
Map<String, Object> responseMap = new HashMap<>(16);
responseMap.put("code", code.getCode());
responseMap.put("msg", QmCode.getMsg(code));
responseMap.put("data", data);
responseMap.put("responseTime", new Date());
return this.parseJsonToResponse(responseMap);
}
/**
* 接口回调方法
*
* @param code QmCode
* @param msg 自定义消息
* @param data 传递数据
* @return
*/
public String sendJSON(QmCode code, String msg, Object data) {
Map<String, Object> responseMap = new HashMap<>(16);
responseMap.put("code", code.getCode());
responseMap.put("msg", msg);
responseMap.put("data", data);
responseMap.put("responseTime", new Date());
return this.parseJsonToResponse(responseMap);
}
/**
* 解析请求json字符串
*
* @param value
* @return
*/
public String parseRequestJson(String value) {
JSONObject jsonObject = JSONObject.parseObject(value);
String json = jsonObject.getString(TransmitConfiguration.requestKey);
if (AesConfiguration.start) {
try {
json = QmAesTools.decryptAES(json);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return json;
}
/**
* 转换json
*
* @param responseMap
* @return
*/
private String parseJsonToResponse(Map<String, Object> responseMap) {
//SerializerFeature.WriteMapNullValue设置后,返回Bean时字段为空时默认返回null
String data = JSONObject.toJSONString(responseMap, SerializerFeature.WriteMapNullValue);
try {
if (AesConfiguration.start) {
Map<String, String> valueMap = new HashMap<>(16);
valueMap.put(TransmitConfiguration.responseKey, QmAesTools.encryptAES(data));
return JSONObject.toJSONString(valueMap, SerializerFeature.WriteMapNullValue);
}
} catch (Exception e) {
throw new QmFrameException("加密失败", e);
}
Map<String, Map<String, Object>> valueMap = new HashMap<>(16);
valueMap.put(TransmitConfiguration.responseKey, responseMap);
return JSONObject.toJSONString(valueMap, SerializerFeature.WriteMapNullValue);
}
}
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
89
90
91
92
93
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
89
90
91
92
93
# 状态码枚举类源码
在调用
QmController
时,我们需要给予send方法中的参数一个枚举QmCode。它封装了我们规定的一套状态码规范。
具体源码如下:
public enum QmCode {
/**
* 成功
*/
_1(1, "操作成功", "Success"),
/**
* 失败
*/
_2(2, "操作失败", "Defeated"),
/**
* 未定义消息
*/
_3(3, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_4(4, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_5(5, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_6(6, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_7(7, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_8(8, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_9(9, "未定义消息", "Undefined message"),
/**
* 未定义消息
*/
_10(10, "未定义消息", "Undefined message"),
/**
* 参数提供不完整
*/
_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120