A unified return value code

package com.sise.wangzhan.commonutils; import lombok.Data; import java.util.HashMap; import java.util.Map; /** * Copyright: Copyright (c) 2020 Asiainfo * * @ClassName: com.sise.wangzhan.commonutils.R * @Description: * <p> * @version: v1.0.0 * @author: wangzhan * @date: 2020/12/30 10:17 * <p> * Modification History: * the Date the Author Version Description * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * 2020/12/30 acer v1.0.0 */ @data public class R {private Boolean success; Private Integer code; Private String Description; Private Map<String, Object> data = new HashMap<String, Object>(); Public static R ok(){R R = new R(); public static R ok(){R R = new R(); r.setSuccess(true); r.setCode(ResultCode.SUCCESS); R.s etDescription (" success "); return r; } public static R error(){R R = new R(); r.setSuccess(false); r.setCode(ResultCode.ERROR); R.s etDescription (" failure "); return r; } public R success(Boolean success){ this.setSuccess(success); return this; } public R code(Integer code){ this.setCode(code); return this; } public R description(String description){ this.setDescription(description); return this; } public R data(String key, Object value){ this.data.put(key, value); return this; } public R data(Map<String, Object> data){ this.setData(data); return this; }}Copy the code

2. Use in controller

Something like this

PostMapping(value = "/login") public R login(@requestBody Member Member){try {// Query whether the user has a Member based on the nickname and password loginMember = memberService.getMemberByNicknameAndPassword(member); If (loginMember == null){return r.ror (). Description (" login failed!! ") ); }else if (loginMember ! = null){// Obtain token String Token = jwTutils.getJwtToken (loginmember.getid (), loginmember.getnickName ()); return R.ok().data("token", token); } } catch (Exception e) { e.printStackTrace(); } return null; }Copy the code

Return to the foreground to receive (I’m using vue.js)

Response.data.data. token (where token is the value of the first parameter passed from the back end)Copy the code