Redux inspired SpringMVC Controller Coding Style with RxJava

when i deal writing springmvc controller，i wonder how to optimize the coding style. commonly，one controller is associated with one business logic。If there are too many conditions，the code may be in a mess and suffer awful maintainability。So i use rxjava and the concept of redux to do that:



String login(HttpServletRequest request, @RequestMapping (“/login/{verifyCode}”)String login(HttpServletRequest request, @PathVariable (“verifyCode”) String verifyCode) { //初始化上下文

SyncContext syncContext = new SyncContext(); syncContext //判断用户提交的信息是否有效

.requestHandler((uniResourceBag, action) -> { JSONObject requestData = UniResourceBag.parseRequestData(request, “username”, “password”); return new Action(“RequestDataReady”)

.addActionData(“username”, requestData.getString(“username”))

.addActionData(“password”, requestData.getString(“password”)); }) .step(“请求处理”) .reducer(“判断用户是否存在”, (uniResourceBag, action) -> { //判断接收到的类型是否为当前需要处理的类型,如果不是则忽略

if (!action.isType(“RequestDataReady”)) {

return new Action();

} String username = (String) action.getActionDataOrDefault(“username”); String password = (String) action.getActionDataOrDefault(“password”); //如果用户名为chevalier,则认为是存在的

if (username.equals(“chevalier”)) { return new Action(“doLogin”).addActionData(“username”, username).addActionData(“password”, password); } else if (username.equals(“error”)) { throw new NullPointerException(“error”); } else {

//否则创建新用户

return new Action(“doRegister”).addActionData(“username”, username).addActionData(“password”, password);

} }) .step(“执行登录操作或者创建新用户”) .reducer(“对于已存在用户进行登录操作,返回user_token”, (uniResourceBag, action) -> { if (!action.isType(“doLogin”)) {

return new Action();

} String username = (String) action.getActionDataOrDefault(“username”); String password = (String) action.getActionDataOrDefault(“password”); //将username+password连接作为user_token登录

uniResourceBag.setRequestDataWhenSuccess(“user_token”, UUID.randomUUID()); return new Action(“Complete”);

}) .reducer(“对于新用户判断用户名是否存在,返回创建好的信息与user_token”, (uniResourceBag, action) -> { if (!action.isType(“doRegister”)) {

return new Action();

} //返回用户名,用户密码,创建时间和用户Token

String username = (String) action.getActionDataOrDefault(“username”); String password = (String) action.getActionDataOrDefault(“password”); //将username+password连接作为user_token登录

uniResourceBag.setRequestDataWhenSuccess(

“username”, username,

“password”, password,

“create_time”, Instant.now().toEpochMilli(),

“user_token”, UUID.randomUUID()); return new Action(“Complete”);

}) .responseHandler((uniResourceBag, action) -> { //进行最后的处理 return new Action();

}); return syncContext

.getUniResourceBag()

.getResponseString(); }

all the code can be found here:https://github.com/wxyyxc1992/SpringMVC-Boilerplate

one typical request is: login/1?requestData={“username”:”chevalier”,”password”:123}

and the result is: