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