返回对象result封装 java
程序员文章站
2024-03-19 23:37:17
...
我们调用服务返回的类型有String,List,Map,Bean,Int,Boolean等类型,可以统一成result对象返回,自带生成Json字符串方法,使用起来非常方便,代码如下:
结合logger,我们可以返回更加清晰的日志信息,帮助我们准确定位出错代码块
import tf56.sofa.serializer.JsonGenerateUtil; /** * 返回对象Result封装 * @athor changmeng.liu * @date 2014-7-25 * @version 1.0 * @update */ public class Result { final String SUCCESS="success"; final String ERROR="error"; private String count=""; /** 响应数据 */ private Object data; /** 响应分页 */ /** 响应状态 */ private Boolean status = true; /** 响应消息 */ private String message; public Result() {} public Result(Object data) { this.data = data; } public Result(String message, Boolean status) { this.set(message, status); } public Result(String message, Boolean status,String count, Object data) { this.set(message, status); this.data = data; this.count=count; } public void set(String message, Boolean status) { this.status = status; this.message = message; this.count=""; this.data=""; } public void set(String message, Boolean status,String count, Object data) { this.status = status; this.message = message; this.data = data; this.count=count; } public Boolean getStatus() { return status; } public void setStatus(Boolean status) { this.status = status; this.message=this.status?"成功":"失败"; this.count=""; this.data=""; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getCommonJson(){ return JsonGenerateUtil.getCommonJson(status==true?SUCCESS:ERROR, message, count==null?"":count, data==null?"":data); } }
public class InvitationCodeServiceImpl implements InvitationCodeService { @Autowired private InvitationCodeDao invitationCodeDao; @Autowired private PartyService partyService; protected Result result=new Result(); protected final Log logger = LogFactory.getLog(getClass()); /** * @athor changmeng.liu * @date 2014-7-15 * @version 1.0 * @function * @param * @update */ @Override public Result getInvitationCodeByTypeAndId(Map map) { String type=(String) map.get("type"); String fromid=(String) map.get("fromid"); if(StringUtils.isEmpty(type)){ this.result.set("类型不能为空", false); logger.debug(this.result.getMessage()); return this.result; } if(StringUtils.isEmpty(fromid)){ this.result.set("来源ID不能为空", false); logger.debug(this.result.getMessage()); return this.result; } if(!"小二".equals(type)&&!"会员".equals(type)){ this.result.set("类型不正确", false); logger.debug(this.result.getMessage()); return this.result; } Map<String,Object> paramMap=new HashMap<String,Object>(); paramMap.put("type", type); paramMap.put("fromid", fromid); InvitationCode invitationCode=invitationCodeDao.selectByTypeAndId(paramMap); if(invitationCode==null){ this.result.set("没有找到该用户的邀请码", true,"",""); logger.debug(this.result.getMessage()); return this.result; } this.result.set("成功", true, "1", invitationCode); return this.result; } }
结合logger,我们可以返回更加清晰的日志信息,帮助我们准确定位出错代码块