Sie sind auf Seite 1von 5

5/1/2016

Redux inspired SpringMVC Controller Coding Style with RxJava Medium

Redux inspired SpringMVC Controller Coding Style with RxJava

i wonder how to optimize the

when i deal writing springmvc controller

one controller is associated with one business


logicIf there are too many conditionsthe code may be in a mess and
suer awful maintainabilitySo i use rxjava and the concept of redux to do
coding style. commonly

that:

@RequestMapping(/login/{verifyCode})
Stringlogin(HttpServletRequestrequest,
@PathVariable(verifyCode)StringverifyCode){
//
SyncContextsyncContext=newSyncContext()
syncContext
//
.requestHandler((uniResourceBag,action)>{
JSONObjectrequestData=UniResourceBag.parseRequestData(request,
username,password)
returnnewAction(RequestDataReady)
.addActionData(username,requestData.getString(username))
.addActionData(password,requestData.getString(password))
})
.step()
.reducer(,(uniResourceBag,action)>{
//,
if(!action.isType(RequestDataReady)){
returnnewAction()
}

https://medium.com/@384924552/redux-inspired-springmvc-controller-coding-style-with-rxjava-11cf87bbee64#.f2a8u31hx

1/5

5/1/2016

Redux inspired SpringMVC Controller Coding Style with RxJava Medium

Stringusername=(String)
action.getActionDataOrDefault(username)
Stringpassword=(String)
action.getActionDataOrDefault(password)
//chevalier,
if(username.equals(chevalier)){
returnnewAction(doLogin).addActionData(username,
username).addActionData(password,password)
}elseif(username.equals(error)){
thrownewNullPointerException(error)
}else{
//
returnnewAction(doRegister).addActionData(username,
username).addActionData(password,password)
}
})
.step()
.reducer(,user_token,(uniResourceBag,
action)>{
if(!action.isType(doLogin)){
returnnewAction()
}
Stringusername=(String)
action.getActionDataOrDefault(username)
Stringpassword=(String)
action.getActionDataOrDefault(password)
//username+passworduser_token
uniResourceBag.setRequestDataWhenSuccess(user_token,
UUID.randomUUID())
returnnewAction(Complete)
})
.reducer(,user_token,
(uniResourceBag,action)>{
if(!action.isType(doRegister)){
returnnewAction()

https://medium.com/@384924552/redux-inspired-springmvc-controller-coding-style-with-rxjava-11cf87bbee64#.f2a8u31hx

2/5

5/1/2016

Redux inspired SpringMVC Controller Coding Style with RxJava Medium

}
//,,Token
Stringusername=(String)
action.getActionDataOrDefault(username)
Stringpassword=(String)
action.getActionDataOrDefault(password)
//username+passworduser_token
uniResourceBag.setRequestDataWhenSuccess(
username,username,
password,password,
create_time,Instant.now().toEpochMilli(),
user_token,UUID.randomUUID())
returnnewAction(Complete)
})
.responseHandler((uniResourceBag,action)>{
//
returnnewAction()
})
returnsyncContext
.getUniResourceBag()
.getResponseString()
}

all the code can be found here:https://github.com/wxyyxc1992/SpringMVCBoilerplate

one typical request is: login/1?requestData=


{username:chevalier,password:123}

and the result is:

{
code:0,
subCode:0,
runtimeLog:{
ContextUptime:2,
steps:[
{

https://medium.com/@384924552/redux-inspired-springmvc-controller-coding-style-with-rxjava-11cf87bbee64#.f2a8u31hx

3/5

5/1/2016

Redux inspired SpringMVC Controller Coding Style with RxJava Medium

actualReducerDesc:RequestHandler,
stepDesc:RequestHandler,
stepId:0,
actualReducerAction:{
actionType:RequestDataReady,
actionData:{
password:123,
username:chevalier
}
},
stepRunTime:0,
actualReducerUUID:2608550a655040da84aaa6a5b68bb93d
},
{
actualReducerDesc:,
stepDesc:,
stepId:1,
actualReducerAction:{
actionType:doLogin,
actionData:{
password:123,
username:chevalier
}
},
stepRunTime:0,
actualReducerUUID:92277976bc244f69b286a4c95be993ac
},
{
actualReducerDesc:,
user_token,
stepDesc:,
stepId:2,
actualReducerAction:{
actionType:Complete,
actionData:{

}
},
stepRunTime:0,
actualReducerUUID:f21b1a809b8649d797515bdc6d90c355
}
]
},
user_token:499206a20130484b82e02b822c3b8dfa,
desc:Success
}

https://medium.com/@384924552/redux-inspired-springmvc-controller-coding-style-with-rxjava-11cf87bbee64#.f2a8u31hx

4/5

5/1/2016

Redux inspired SpringMVC Controller Coding Style with RxJava Medium

https://medium.com/@384924552/redux-inspired-springmvc-controller-coding-style-with-rxjava-11cf87bbee64#.f2a8u31hx

5/5

Das könnte Ihnen auch gefallen