标签:body set 操作 pos 模拟 false ret pass get
1.AjaxResult一般使用 success/msg即可,但有时需求需要返回后台对象时,需要进行如下操作
/**
* 返回结果
*/
public class AjaxResult {
public static void main(String[] args) {
//封装AjaxResult--返回后台数据
AjaxResult.me().setSuccess(true).setMsg("恭喜你操作成功").setObject("你运气好,账号密码都是对的");
}
public static AjaxResult me(){
return new AjaxResult();
}
private Boolean success = true;//默认操作成功
private String msg = "操作成功";//返回前端操作的文字结果
private Object object;//返回后台的对象
public Boolean getSuccess() {
return success;
}
public AjaxResult setSuccess(Boolean success) {
this.success = success;
return this;
}
public String getMsg() {
return msg;
}
public AjaxResult setMsg(String msg) {
this.msg = msg;
return this;
}
public Object getObject() {
return object;
}
public AjaxResult setObject(Object object) {
this.object = object;
return this;
}
@Override
public String toString() {
return "AjaxResult{" +
"success=" + success +
", msg=‘" + msg + ‘\‘‘ +
", object=" + object +
‘}‘;
}
}
2.测试
public class UserController {
/*
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的
*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public AjaxResult login(@RequestBody User user){
//模拟登录--假设已经从数据库中查出来账号密码
if(user!=null&& !StringUtils.isEmpty(user.getName())&&!StringUtils.isEmpty(user.getPassword())){
if("admin".equals(user.getName())&&"1234".equals(user.getPassword())){
return AjaxResult.me().setSuccess(true).setMsg("登录成功").setObject(null);
}
}
return AjaxResult.me().setSuccess(false).setMsg("登录失败").setObject("账号密码不正确");
}
}
标签:body set 操作 pos 模拟 false ret pass get
原文地址:https://www.cnblogs.com/wgyi140724-/p/10604664.html