11
11
12
12
package cn .wubo .apijson .dynamic .datasource .controller ;
13
13
14
- import java .net .URLDecoder ;
15
- import java .util .ArrayList ;
16
- import java .util .List ;
17
- import java .util .Map ;
18
-
19
- import javax .servlet .http .HttpSession ;
20
-
14
+ import apijson .RequestMethod ;
15
+ import apijson .StringUtil ;
16
+ import apijson .framework .APIJSONController ;
17
+ import apijson .orm .Parser ;
21
18
import cn .wubo .apijson .dynamic .datasource .controller .dto .RecordDTO ;
22
19
import cn .wubo .apijson .dynamic .datasource .framework .ADDTransactional ;
23
20
import com .alibaba .fastjson .JSON ;
24
21
import com .alibaba .fastjson .JSONArray ;
25
22
import com .alibaba .fastjson .JSONObject ;
26
- import org .springframework .web .bind .annotation .GetMapping ;
27
- import org .springframework .web .bind .annotation .PathVariable ;
28
- import org .springframework .web .bind .annotation .PostMapping ;
29
- import org .springframework .web .bind .annotation .RequestBody ;
30
- import org .springframework .web .bind .annotation .RequestMapping ;
31
- import org .springframework .web .bind .annotation .RequestParam ;
32
- import org .springframework .web .bind .annotation .RestController ;
23
+ import jakarta .servlet .http .HttpSession ;
24
+ import org .springframework .web .bind .annotation .*;
33
25
34
- import apijson .RequestMethod ;
35
- import apijson .StringUtil ;
36
- import apijson .framework .APIJSONController ;
37
- import apijson .orm .Parser ;
26
+ import java .net .URLDecoder ;
27
+ import java .util .List ;
28
+ import java .util .Map ;
38
29
39
30
40
- /**请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
31
+ /**
32
+ * 请求路由入口控制器,包括通用增删改查接口等,转交给 APIJSON 的 Parser 来处理
41
33
* 具体见 SpringBoot 文档
42
34
* https://www.springcloud.cc/spring-boot.html#boot-features-spring-mvc
43
35
* 以及 APIJSON 通用文档 3.设计规范 3.1 操作方法
46
38
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
47
39
* <br > 2.提高性能 - 无需URL encode和decode
48
40
* <br > 3.调试方便 - 建议使用 APIAuto(http://apijson.cn/api) 或 Postman
41
+ *
49
42
* @author Lemon
50
43
*/
51
44
@ RestController
52
45
@ RequestMapping ("" )
53
46
public class DemoController extends APIJSONController <Long > {
54
47
55
- @ Override
56
- public Parser <Long > newParser (HttpSession session , RequestMethod method ) {
57
- return super .newParser (session , method ).setNeedVerify (false ); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
58
- }
48
+ @ Override
49
+ public Parser <Long > newParser (HttpSession session , RequestMethod method ) {
50
+ return super .newParser (session , method ).setNeedVerify (false ); // TODO 这里关闭校验,方便新手快速测试,实际线上项目建议开启
51
+ }
59
52
60
- /**
61
- * 批量处理增删改查
62
- * @param records
63
- * @param session
64
- * @return
65
- */
66
- @ PostMapping (value = "saveBatch" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
67
- @ ADDTransactional
68
- public JSONArray crud (@ RequestBody List <RecordDTO > records , HttpSession session ) {
69
- JSONArray ja = new JSONArray ();
70
- for (RecordDTO rec : records ) {
71
- JSONObject jo = JSON .parseObject (super .crud (rec .getMethod (), rec .getData ().toJSONString (), session ));
72
- if (jo .containsKey ("code" ) && jo .getInteger ("code" ) != 200 ) throw new RuntimeException (jo .getString ("msg" ));
73
- ja .add (jo );
74
- }
75
- return ja ;
76
- }
53
+ /**
54
+ * 批量处理增删改查
55
+ *
56
+ * @param records
57
+ * @param session
58
+ * @return
59
+ */
60
+ @ PostMapping (value = "saveBatch" )
61
+ // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
62
+ @ ADDTransactional
63
+ public JSONArray crud (@ RequestBody List <RecordDTO > records , HttpSession session ) {
64
+ JSONArray ja = new JSONArray ();
65
+ for (RecordDTO rec : records ) {
66
+ JSONObject jo = JSON .parseObject (super .crud (rec .getMethod (), rec .getData ().toJSONString (), session ));
67
+ if (jo .containsKey ("code" ) && jo .getInteger ("code" ) != 200 ) throw new RuntimeException (jo .getString ("msg" ));
68
+ ja .add (jo );
69
+ }
70
+ return ja ;
71
+ }
77
72
78
- /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
79
- * @param method
80
- * @param request
81
- * @param session
82
- * @return
83
- */
84
- @ PostMapping (value = "{method}" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
85
- @ Override
86
- public String crud (@ PathVariable String method , @ RequestBody String request , HttpSession session ) {
87
- return super .crud (method , request , session );
88
- }
73
+ /**
74
+ * 增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
75
+ *
76
+ * @param method
77
+ * @param request
78
+ * @param session
79
+ * @return
80
+ */
81
+ @ PostMapping (value = "{method}" )
82
+ // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method} 或 Controller 注解 @RequestMapping("crud")
83
+ @ Override
84
+ public String crud (@ PathVariable String method , @ RequestBody String request , HttpSession session ) {
85
+ return super .crud (method , request , session );
86
+ }
89
87
90
- /**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
91
- * @param method
92
- * @param tag
93
- * @param params
94
- * @param request
95
- * @param session
96
- * @return
97
- */
98
- @ PostMapping ("{method}/{tag}" ) // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
99
- @ Override
100
- public String crudByTag (@ PathVariable String method , @ PathVariable String tag , @ RequestParam Map <String , String > params , @ RequestBody String request , HttpSession session ) {
101
- return super .crudByTag (method , tag , params , request , session );
102
- }
88
+ /**
89
+ * 增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
90
+ *
91
+ * @param method
92
+ * @param tag
93
+ * @param params
94
+ * @param request
95
+ * @param session
96
+ * @return
97
+ */
98
+ @ PostMapping ("{method}/{tag}" )
99
+ // 如果和其它的接口 URL 冲突,可以加前缀,例如改为 crud/{method}/{tag} 或 Controller 注解 @RequestMapping("crud")
100
+ @ Override
101
+ public String crudByTag (@ PathVariable String method , @ PathVariable String tag , @ RequestParam Map <String , String > params , @ RequestBody String request , HttpSession session ) {
102
+ return super .crudByTag (method , tag , params , request , session );
103
+ }
103
104
104
- /**获取
105
- * 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
106
- * @param request 只用String,避免encode后未decode
107
- * @param session
108
- * @return
109
- * @see {@link RequestMethod#GET}
110
- */
111
- @ GetMapping ("get/{request}" )
112
- public String openGet (@ PathVariable String request , HttpSession session ) {
113
- try {
114
- request = URLDecoder .decode (request , StringUtil .UTF_8 );
115
- } catch (Exception e ) {
116
- // Parser 会报错
117
- }
118
- return get (request , session );
119
- }
105
+ /**
106
+ * 获取
107
+ * 只为兼容HTTP GET请求,推荐用HTTP POST,可删除
108
+ *
109
+ * @param request 只用String,避免encode后未decode
110
+ * @param session
111
+ * @return
112
+ * @see {@link RequestMethod#GET}
113
+ */
114
+ @ GetMapping ("get/{request}" )
115
+ public String openGet (@ PathVariable String request , HttpSession session ) {
116
+ try {
117
+ request = URLDecoder .decode (request , StringUtil .UTF_8 );
118
+ } catch (Exception e ) {
119
+ // Parser 会报错
120
+ }
121
+ return get (request , session );
122
+ }
120
123
121
124
}
0 commit comments