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 org .springframework .web .bind .annotation .GetMapping ;
18+ import org .springframework .web .bind .annotation .PathVariable ;
19+ import org .springframework .web .bind .annotation .PostMapping ;
20+ import org .springframework .web .bind .annotation .RequestBody ;
21+ import org .springframework .web .bind .annotation .RequestMapping ;
22+ import org .springframework .web .bind .annotation .RequestParam ;
23+ import org .springframework .web .bind .annotation .RestController ;
24+
25+ import java .net .URLDecoder ;
26+ import java .util .Map ;
27+
28+ import javax .servlet .http .HttpSession ;
29+
30+ import apijson .RequestMethod ;
31+ import apijson .StringUtil ;
32+ import apijson .framework .APIJSONController ;
33+ import apijson .orm .Parser ;
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 Parser <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 String method , @ RequestBody String request , HttpSession session ) {
65+ return super .crud (method , request , session );
66+ }
67+
68+ /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
69+ * @param method
70+ * @param tag
71+ * @param params
72+ * @param request
73+ * @param session
74+ * @return
75+ */
76+ @ PostMapping ("{method}/{tag}" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
77+ @ Override
78+ public String crudByTag (@ PathVariable String method , @ PathVariable String tag , @ RequestParam Map <String , String > params , @ RequestBody String request , HttpSession session ) {
79+ return super .crudByTag (method , tag , params , request , session );
80+ }
81+
82+ /**获取
83+ * 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
84+ * @param request 只用String,避免encode后未decode
85+ * @param session
86+ * @return
87+ * @see {@link RequestMethod#GET}
88+ */
89+ @ GetMapping ("get/{request}" )
90+ public String openGet (@ PathVariable String request , HttpSession session ) {
91+ try {
92+ request = URLDecoder .decode (request , StringUtil .UTF_8 );
93+ } catch (Exception e ) {
94+ // Parser 会报错
95+ }
96+ return get (request , session );
97+ }
98+
99+ }
0 commit comments