1)从数据库到前端,做了三层转换,最后统一返回给前端的格式
DO-> model:
放在service中,目的是为了组装来自于数据库的数据,有些字段来自于不同的表的取,这一层相当于真正的业务模型
->VO
放在controller中,目的是返回给前端真正需要的数据,而不是返回所有的Model中的数据
->commonReturnType(统一返回的格式)
放在独立的包中,用于处理放回给前端固定的数据类型,以便于前端坐判断处理以及提示
2)创建一个error的包:用于存放公共的错误码,异常拦截(正常异常时会抛到tomcat容器中前端的status是500,这里拦击设置status 200)
CommonError 接口类定义需要实现的方法
EmBusinessError 实现CommonError接口,定义错误码
BusinessException 继承Exception并且实现CommonError类,用于抛出业务异常
3)在Controller层添加BaseController类
该类用于拦截异常,设置status为ok,这里如果不拦截那么前端获取到的是500拿不到任何信息,判断是否是业务类异常,如果是使用相应的 code码返回,如果不是那么也应该给予前端相应的code码,以便前端更好的提示用户
下面是实现的代码:
UserModel
package com.miaoshaproject.service.model; public class UserModel {
private Integer id; private String name; private String gender; private Integer age; private String registerMode; private Integer thirdPartyId; private String telphone; private String enCrptPassword; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getRegisterMode() {
return registerMode;
} public void setRegisterMode(String registerMode) {
this.registerMode = registerMode;
} public Integer getThirdPartyId() {
return thirdPartyId;
} public void setThirdPartyId(Integer thirdPartyId) {
this.thirdPartyId = thirdPartyId;
} public String getTelphone() {
return telphone;
} public void setTelphone(String telphone) {
this.telphone = telphone;
} public String getEnCrptPassword() {
return enCrptPassword;
} public void setEnCrptPassword(String enCrptPassword) {
this.enCrptPassword = enCrptPassword;
}
}
UserVO
package com.miaoshaproject.controller.viewobject; public class UserVO {
private Integer id; private String name; private String gender; private Integer age; private String registerMode; private Integer thirdPartyId; private String telphone; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getRegisterMode() {
return registerMode;
} public void setRegisterMode(String registerMode) {
this.registerMode = registerMode;
} public Integer getThirdPartyId() {
return thirdPartyId;
} public void setThirdPartyId(Integer thirdPartyId) {
this.thirdPartyId = thirdPartyId;
} public String getTelphone() {
return telphone;
} public void setTelphone(String telphone) {
this.telphone = telphone;
}
}
CommonReturnType
package com.miaoshaproject.response; public class CommonReturnType {
private String status;
private Object data; public static CommonReturnType create(Object data){
return CommonReturnType.create(data,"success");
} public static CommonReturnType create(Object data,String status){
CommonReturnType type=new CommonReturnType();
type.setData(data);
type.setStatus(status);
return type;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
}
}
CommonError
package com.miaoshaproject.error; public interface CommonError {
public int getErrCode();
public String getErrMsg();
public CommonError setErrMsg(String errMsg);
}
EmBusinessError
package com.miaoshaproject.error; public enum EmBusinessError implements CommonError {
USER_NOT_EXIST(10001,"用户不存在"),
UNKOWN_ERROR(10002,"未知错误"),
PARAMTER_VALIDATION_ERROR(20001,"参数校验不通过")
;
private int errCode;
private String errMsg; private EmBusinessError(int errCode,String errMsg){
this.errCode=errCode;
this.errMsg=errMsg;
} @Override
public int getErrCode() {
return this.errCode;
} @Override
public String getErrMsg() {
return this.errMsg;
} @Override
public CommonError setErrMsg(String errMsg) {
this.errMsg=errMsg;
return this;
}
}
BusinessException
package com.miaoshaproject.error;
//包装器业务异常类实现
public class BusinessException extends Exception implements CommonError{
private CommonError commonError;
//直接接收EmBusinessError的传参用于构造业务异常
public BusinessException(CommonError commonError){
super();
this.commonError=commonError;
}
//直接接收EmBusinessError,errMsg的的传参用于构造业务异常
public BusinessException(CommonError commonError,String errMsg){
super();
this.commonError=commonError;
this.commonError.setErrMsg(errMsg);
}
@Override
public int getErrCode() {
return this.commonError.getErrCode();
} @Override
public String getErrMsg() {
return this.commonError.getErrMsg();
} @Override
public CommonError setErrMsg(String errMsg) {
this.commonError.setErrMsg(errMsg);
return this;
}
}
BaseController
package com.miaoshaproject.controller; import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.response.CommonReturnType;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map; public class BaseController {
@ExceptionHandler
@ResponseStatus (HttpStatus.OK)
@ResponseBody
public CommonReturnType handlerException(HttpServletRequest request, Exception ex){
Map<String,Object> responseData = new HashMap<>();
if(ex instanceof BusinessException){
BusinessException businessException = (BusinessException)ex;
responseData.put("errCode",businessException.getErrCode());
responseData.put("errMsg",businessException.getErrMsg());
}else{
responseData.put("errCode", EmBusinessError.UNKOWN_ERROR.getErrCode());
responseData.put("errMsg",EmBusinessError.UNKOWN_ERROR.getErrMsg());
}
return CommonReturnType.create(responseData,"fail");
}
}