1+ /*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+ Licensed under the Apache License, Version 2.0 (the "License");
4+ you may not use this file except in compliance with the License.
5+ You may obtain a copy of the License at
6+
7+ http://www.apache.org/licenses/LICENSE-2.0
8+
9+ Unless required by applicable law or agreed to in writing, software
10+ distributed under the License is distributed on an "AS IS" BASIS,
11+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ See the License for the specific language governing permissions and
13+ limitations under the License.*/
14+
15+ package apijson .demo ;
16+
17+ import apijson .RequestMethod ;
18+ import apijson .StringUtil ;
19+ import jakarta .servlet .http .HttpSession ;
20+ import org .springframework .web .bind .annotation .*;
21+
22+ import java .net .URLDecoder ;
23+
24+
25+ /**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
26+ * 具体见 SpringBoot 文档
27+ * https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
28+ * 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
29+ * https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
30+ * <br > 建议全通过HTTP POST来请求:
31+ * <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
32+ * <br > 2.提高性能 - 无需URL encode和decode
33+ * <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
34+ * @author Lemon
35+ */
36+ @ RestController
37+ @ RequestMapping ("" )
38+ public class DemoController { // extends APIJSONController<Long> {
39+
40+ public String parse (RequestMethod method , String request , HttpSession session ) {
41+ // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
42+ return new DemoParser (method ).setSession (session ).setNeedVerify (false ).parse (request );
43+ }
44+
45+
46+ //通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
47+
48+ /**获取
49+ * @param request 只用String,避免encode后未decode
50+ * @param session
51+ * @return
52+ * @see {@link RequestMethod#GET}
53+ */
54+ @ PostMapping (value = "get" )
55+ public String get (@ RequestBody String request , HttpSession session ) {
56+ return parse (RequestMethod .GET , request , session );
57+ }
58+
59+
60+ /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
61+ * @param method
62+ * @param request
63+ * @param session
64+ * @return
65+ */
66+ @ PostMapping (value = "{method}" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
67+ // @Override
68+ public String crud (@ PathVariable String method , @ RequestBody String request , HttpSession session ) {
69+ return parse (RequestMethod .valueOf (StringUtil .toUpperCase (method )), request , session );
70+ }
71+
72+ /**获取
73+ * 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
74+ * @param request 只用String,避免encode后未decode
75+ * @param session
76+ * @return
77+ * @see {@link RequestMethod#GET}
78+ */
79+ @ GetMapping ("get/{request}" )
80+ public String openGet (@ PathVariable String request , HttpSession session ) {
81+ try {
82+ request = URLDecoder .decode (request , StringUtil .UTF_8 );
83+ } catch (Exception e ) {
84+ // Parser 会报错
85+ }
86+ return get (request , session );
87+ }
88+
89+ }
0 commit comments