-
Notifications
You must be signed in to change notification settings - Fork 261
/
DemoController.java
111 lines (95 loc) · 4.39 KB
/
DemoController.java
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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo;
import java.net.URLDecoder;
import java.util.Map;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.framework.APIJSONController;
import apijson.orm.Parser;
/**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
* 具体见 SpringBoot 文档
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
* https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
* <br > 建议全通过HTTP POST来请求:
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
* <br > 2.提高性能 - 无需URL encode和decode
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
* @author Lemon
*/
@RestController
@RequestMapping("")
public class DemoController extends APIJSONController<Long> {
@Override
public Parser<Long> newParser(HttpSession session, RequestMethod method) {
return super.newParser(session, method).setNeedVerify(false); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
}
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param request
* @param session
* @return
*/
@PostMapping(value = "{method}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
@Override
public String crud(@PathVariable("method") String method, @RequestBody String request, HttpSession session) {
return super.crud(method, request, session);
}
/**全能增删改查接口,可同时进行 增、删、改、查 多种操作,
* 通过 @method: "POST", @gets: { "Privacy":"Privacy-CIRCLE", "User": { "@role":"LOGIN", "tag":"User" } } 等关键词指定
* @param request
* @param session
* @return
*/
@PostMapping(value = "crud") // 直接 {method} 或 apijson/{method} 会和内置网页的路由有冲突
// @Override
public String crudAll(@RequestBody String request, HttpSession session) {
return newParser(session, RequestMethod.CRUD).parse(request);
}
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param tag
* @param params
* @param request
* @param session
* @return
*/
@PostMapping("{method}/{tag}") // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
@Override
public String crudByTag(@PathVariable("method") String method, @PathVariable("tag") String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
return super.crudByTag(method, tag, params, request, session);
}
/**获取
* 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
* @param request 只用String,避免encode后未decode
* @param session
* @return
* @see {@link RequestMethod#GET}
*/
@GetMapping("get/{request}")
public String openGet(@PathVariable("request") String request, HttpSession session) {
try {
request = URLDecoder.decode(request, StringUtil.UTF_8);
} catch (Exception e) {
// Parser 会报错
}
return get(request, session);
}
}