1.在pom.xml中增加maven资源
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.13</version> </dependency>
2.在common包中创建类文件ResultUtils.java
package sinosoft.bjredcross.common; import com.alibaba.fastjson.JSON; public class ResultUtils { private String msg; private boolean success = true; private Object result; public void setSuccess(boolean success) { this.success = success; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getResult() { return result; } public void setResult(Object result) { this.result = result; } public ResultUtils() { } public ResultUtils(boolean success, String msg) { this.success = success; this.msg = msg; } public ResultUtils(boolean success, String msg, Object result) { this.success = success; this.msg = msg; this.result = result; } public boolean isSuccess() { return success; } public Object successResult(){ ResultUtils resultUtils = new ResultUtils(); resultUtils.setSuccess(true); resultUtils.setMsg("success"); resultUtils.setResult(null); return JSON.toJSON(resultUtils); } public Object successResult(Object obj){ if (obj instanceof String){ return successResult(null, (String) obj); }else{ return successResult(obj, "error"); } } public Object successResult(Object obj, String msg){ ResultUtils resultUtils = new ResultUtils(); resultUtils.setSuccess(true); resultUtils.setMsg(msg); resultUtils.setResult(obj); return JSON.toJSON(resultUtils); } public Object errorResult(){ ResultUtils resultUtils = new ResultUtils(); resultUtils.setSuccess(false); resultUtils.setMsg("error"); resultUtils.setResult(null); return JSON.toJSON(resultUtils); } /* * 杩斿洖閿欒缁撴灉 * */ public Object errorResult(Object obj){ if (obj instanceof String){ return renderJsonError(null, (String) obj); }else{ return renderJsonError(obj, "error"); } } public Object renderJsonError(Object obj, String msg){ ResultUtils resultUtils = new ResultUtils(); resultUtils.setSuccess(false); resultUtils.setMsg(msg); resultUtils.setResult(obj); return JSON.toJSON(resultUtils); } }