@RequestMapping(value="/login" ,method = RequestMethod.POST)
public BaseResponse login(@RequestBody UserModel userModel){
BaseResponse resp = new BaseResponse();
try{
resp.setSuccess(true);
if(!userService.login(userModel)){
resp.setSuccess(false);
resp.setMsg("login failed! User name and password do not match");
resp.setErrorCode(ErrorCodeConstants.INTERNAL_ERROR);
}
}catch(BusinessException e){
resp.setSuccess(false);
resp.setMsg(e.getMessage());
resp.setErrorCode(e.getCode());
}
return resp;
}
改造后:
@RequestMapping(value="/login" ,method = RequestMethod.POST)
public BaseResponse login(@RequestBody String body){
BaseResponse resp = new BaseResponse();
try{
Gson gson = new Gson();
Map obj = (Map)JSONObject.fromObject(body);
if (!(obj.get("userName") instanceof String)){
thrownew BusinessException(ErrorCodeConstants.ARGUMENT_FAILURE,"login failed! The userName parameter must be of type String");
} elseif (!(obj.get("password") instanceof String)){
thrownew BusinessException(ErrorCodeConstants.ARGUMENT_FAILURE,"login failed! The password parameter must be of type String");
} elseif (!(obj.get("uuid") instanceof String)){
thrownew BusinessException(ErrorCodeConstants.ARGUMENT_FAILURE,"login failed! The uuid parameter must be of type String");
}
UserModel userModel = gson.fromJson(body, UserModel.class);
resp.setSuccess(true);
if(!userService.login(userModel)){
resp.setSuccess(false);
resp.setMsg("login failed! User name and password do not match");
resp.setErrorCode(ErrorCodeConstants.INTERNAL_ERROR);
}
}catch(BusinessException e){
resp.setSuccess(false);
resp.setMsg(e.getMessage());
resp.setErrorCode(e.getCode());
}
return resp;
}