由于采用前后端分离的开发方式,web层需返回JSON形式的数据。
思路
- 建立通用 请求响应Bean
- 使用jackson库包装请求响应Bean
- 尽可能使用注解方式
代码
-
jackson 依赖
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
-
请求响应Bean
ResponseBean.java
package com.zp.haveplace.bean;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.zp.haveplace.common.ResponseBeanCode;
import com.zp.haveplace.common.ResponseBeanType;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 请求响应Bean
* 使用JSON包装请求返回,使用jackson库
* 加入方法链支持(by zp 2018/4/20)
* @author zwp
* @date 2018/1/22
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseBean implements Serializable {
private String type ;
private int code ;
private String message ;
private Map<String,Object> data = new HashMap<String, Object>();
/**
* 自定义返回
* @param type
* @param code
* @param message
* @return
*/
public ResponseBean setResponse(String type, int code, String message){
this.type = type;
this.code = code;
this.message = message;
return this;
}
public ResponseBean setSuccessResponse(String message){
this.type = ResponseBeanType.SUCCESS ;
this.code = ResponseBeanCode.SUCCESS ;
this.message = message ;
return this;
}
public ResponseBean setErrorResponse(String message){
this.type = ResponseBeanType.ERROR ;
this.code = ResponseBeanCode.ERROR ;
this.message = message ;
return this;
}
public ResponseBean setExceptionResponse(String message){
this.type = ResponseBeanType.ERROR ;
this.code = ResponseBeanCode.ERROR ;
this.message = message ;
return this;
}
public ResponseBean setWarnResponse(String message) {
this.type = ResponseBeanType.WARN ;
this.code = ResponseBeanCode.WARN ;
this.message = message ;
return this;
}
public ResponseBean setNoLoginResponse(String message){
this.type = ResponseBeanType.ERROR ;
this.code = ResponseBeanCode.NO_LOGIN ;
this.message = message ;
return this;
}
public ResponseBean setNoPowerResponse(String message){
this.type = ResponseBeanType.ERROR ;
this.code = ResponseBeanCode.NO_POWER ;
this.message = message ;
return this;
}
public Object getData(String key){
return this.data.get(key);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> data) {
this.data = data;
}
public void setData(String key,Object obj){
this.data.put(key,obj);
}
public void addData(String key,Object obj){
this.data.put(key,obj);
}
}
@JsonInclude(JsonInclude.Include.NON_NULL) 表示不解析为null的字段。
-
一些常量
ResponseBeanCode.java
package com.zp.haveplace.common;
/**
* Created by zwp on 2018/1/22.
* ResponseBean返回码
*/
public class ResponseBeanCode {
//请求成功
public static int SUCCESS = 2000 ;
//未登录
public static int NO_LOGIN = 4003 ;
//没有权限
public static int NO_POWER = 4001;
//请求失败
public static int ERROR = 5000 ;
public static int EXCEPTION = 4005;
public static int WARN = 2001;
}
ResponseBeanType.java
package com.zp.haveplace.common;
/**
* Created by zwp on 2018/1/22.
* ResponseBean返回类型
*/
public class ResponseBeanType {
public static String SUCCESS = "SUCCESS" ;
public static String ERROR = "ERROR" ;
public static String EXCEPTION = "EXCEPTION" ;
public static String WARN = "WARN";
}
- Controller层方法使用范例
package com.zp.haveplace.web;
import com.zp.haveplace.annotation.LoginRequired;
import com.zp.haveplace.annotation.RoleRequired;
import com.zp.haveplace.bean.PageBean;
import com.zp.haveplace.bean.ResponseBean;
import com.zp.haveplace.common.RoleConst;
import com.zp.haveplace.common.SessionConstant;
import com.zp.haveplace.entity.Admin;
import com.zp.haveplace.service.AdminService;
import com.zp.haveplace.bean.LoggerBean;
import com.zp.haveplace.util.TimeUtils;
import com.zp.haveplace.validator.group.admin.LoginValidatorGroup;
import com.zp.haveplace.validator.group.admin.UpdateAdminValidatorGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.acl.Group;
import java.util.EnumSet;
import java.util.List;
/**
* 管理员控制器
*
* @author zp
* @date 2018/4/21
*/
@Controller
@RequestMapping("/admin")
public class AdminController{
// 用户登录方法
@RequestMapping(value = "/login",method = RequestMethod.POST)
@ResponseBody
public ResponseBean login(HttpServletRequest request)throws Exception{
//.....业务逻辑代码
// 1. 返回成功,附带数据
ResponseBean rest = new ResponseBean().setSuccessResponse("登录成功");
rest.getData().put("loginAccount",account);//可添加一些需要显示的数据
rest.getData().put("role",1);//登录人权限
rest.getData().put("loginTime",new Date());//登录时间
//return rest;//Controller方法返回
// 2. 返回失败,不附带数据
//return new ResponseBean().setErrorResponce("请求失败");
return rest;
}
}
实际返回结果
- 返回成功,附带数据
JSON
{
"type": "SUCCESS",
"code": 2000,
"message": "登录成功",
"data": {
"loginAccount": "admin_1",
"role": 1,
"loginTime": "2018-04-23 10:06:35"
}
}
- 返回失败,不附带数据
JSON
{
"type":"ERROR",
"code":5000,
"message":"请求失败",
"data":{}
}
网友评论