1+ /*Copyright ©2025 APIJSON(https://github.com/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 java .net .URLDecoder ;
18+ import java .util .Map ;
19+
20+ import apijson .fastjson2 .APIJSONParser ;
21+ import jakarta .servlet .http .HttpSession ;
22+
23+ import org .springframework .web .bind .annotation .GetMapping ;
24+ import org .springframework .web .bind .annotation .PathVariable ;
25+ import org .springframework .web .bind .annotation .PostMapping ;
26+ import org .springframework .web .bind .annotation .RequestBody ;
27+ import org .springframework .web .bind .annotation .RequestMapping ;
28+ import org .springframework .web .bind .annotation .RequestParam ;
29+ import org .springframework .web .bind .annotation .RestController ;
30+
31+ import apijson .RequestMethod ;
32+ import apijson .StringUtil ;
33+ import apijson .fastjson2 .APIJSONController ;
34+
35+
36+ /**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
37+ * 具体见 SpringBoot 文档
38+ * https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
39+ * 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
40+ * https://github.com/Tencent/APIJSON/blob/master/Document.md#3.1
41+ * <br > 建议全通过HTTP POST来请求:
42+ * <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
43+ * <br > 2.提高性能 - 无需URL encode和decode
44+ * <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
45+ * @author Lemon
46+ */
47+ @ RestController
48+ @ RequestMapping ("" )
49+ public class DemoController extends APIJSONController <Long > {
50+
51+ @ Override
52+ public APIJSONParser <Long > newParser (HttpSession session , RequestMethod method ) {
53+ return super .newParser (session , method ).setNeedVerify (false ); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
54+ }
55+
56+ /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
57+ * @param method
58+ * @param request
59+ * @param session
60+ * @return
61+ */
62+ @ PostMapping (value = "{method}" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
63+ @ Override
64+ public String crud (@ PathVariable ("method" ) String method , @ RequestBody String request , HttpSession session ) {
65+ return super .crud (method , request , session );
66+ }
67+
68+ /**全能增删改查接口,可同时进行 增、删、改、查 多种操作,
69+ * 通过 @method: "POST", @gets: { "Privacy":"Privacy-CIRCLE", "User": { "@role":"LOGIN", "tag":"User" } } 等关键词指定
70+ * @param request
71+ * @param session
72+ * @return
73+ */
74+ @ PostMapping (value = "crud" ) // 直接 {method} 或 apijson/{method} 会和内置网页的路由有冲突
75+ // @Override
76+ public String crudAll (@ RequestBody String request , HttpSession session ) {
77+ return newParser (session , RequestMethod .CRUD ).parse (request );
78+ }
79+
80+ /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
81+ * @param method
82+ * @param tag
83+ * @param params
84+ * @param request
85+ * @param session
86+ * @return
87+ */
88+ @ PostMapping ("{method}/{tag}" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
89+ @ Override
90+ public String crudByTag (@ PathVariable ("method" ) String method , @ PathVariable ("tag" ) String tag , @ RequestParam Map <String , String > params , @ RequestBody String request , HttpSession session ) {
91+ return super .crudByTag (method , tag , params , request , session );
92+ }
93+
94+ /**获取
95+ * 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
96+ * @param request 只用String,避免encode后未decode
97+ * @param session
98+ * @return
99+ * @see {@link RequestMethod#GET}
100+ */
101+ @ GetMapping ("get/{request}" )
102+ public String openGet (@ PathVariable ("request" ) String request , HttpSession session ) {
103+ try {
104+ request = URLDecoder .decode (request , StringUtil .UTF_8 );
105+ } catch (Exception e ) {
106+ // Parser 会报错
107+ }
108+ return get (request , session );
109+ }
110+
111+ }
0 commit comments