1.自定义MyInfoException工具类extneds后端RunTimeException工具类
public class MyInfoException extends RuntimeException {
private int code;
public MyInfoException(int code, String msg) {
super(msg);
this.code = code;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
2.统一的响应类
public class ObjectRestResponse<T> extends BaseResponse {
T data;
public ObjectRestResponse( ) {
}
public ObjectRestResponse data(T data) {
this.setData(data);
return this;
}
public ObjectRestResponse(int resultCode, String resultMsg, T data) {
this.setResultCode(resultCode);
this.setResultMsg(resultMsg);
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public void setErrorMsg(String msg) {
this.setResultCode(RestCodeConstants.EX_OTHER_CODE);
this.setResultMsg(msg);
}
public void setSuucessMsg(String msg) {
this.setResultCode(RestCodeConstants.RESULT_SUCCESS);
this.setResultMsg(msg);
}
public static void main(String[] args) {
List<SelectVo> list = new ArrayList<SelectVo>();
SelectVo selectVo = new SelectVo();
selectVo.setValue("1234");
selectVo.setOption("op1");
list.add(selectVo);
SelectVo selectVo1 = new SelectVo();
selectVo1.setValue("332");
selectVo1.setOption("op2");
list.add(selectVo1);
ObjectRestResponse<List<SelectVo>> response = new ObjectRestResponse<List<SelectVo>>();
response.setData(list);
response.setResultCode(0);
response.setResultMsg("success");
System.out.println(JSON.toJSONString(response));
}
}
2.校验类的封装
2.1后端的controller层会调用这个方法,可以知道如果异常会默认抛出自定义的异常码500,和相关的方法内的异常消息。
package com.citicpru.clmmaint.biz;
import com.citicpru.clmmaint.utils.CheckUtils;
import com.citicpru.clmmaint.utils.ConvertUtil;
import com.citicpru.clmmaint.utils.SearchConditionUtil;
import com.citicpru.clmwork.vo.SearchCondition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
@Component
public class AdvancedSearchBiz {
public StringBuffer searchParamers(List<SearchCondition> searchConditionList) throws Exception {
StringBuffer searchParamer = new StringBuffer();
String space = " ";
for (SearchCondition searchCondition : searchConditionList ) {
String field = ConvertUtil.doLowerCase(searchCondition.getField());
String searchValue = searchCondition.getSearchValue();
String value =ConvertUtil.toConvert(searchValue);
String searchVal;
if(CheckUtils.isNumber(value) == false){
searchVal = "'"+value+"'";
}else{
searchVal = value;
}
String condition = searchCondition.getCondition();
String relation = searchCondition.getRelation();
if (condition.equals(SearchConditionUtil.EQUALS)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(searchVal).
append(space);
}
if (condition.equals(SearchConditionUtil.NOT_EQUALS)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(searchVal).
append(space);
}
if (condition.equals(SearchConditionUtil.GREATER_THAN)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(searchVal).
append(space);
}
if (condition.equals(SearchConditionUtil.LESS_THAN)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(searchVal).
append(space);
}
if (condition.equals(SearchConditionUtil.GREATER_EQUALS)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(searchVal).
append(space);
}
if (condition.equals(SearchConditionUtil.LESS_EQUALS)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(searchVal).
append(space);
}
if (condition.equals(SearchConditionUtil.IN)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append("(")
.append(space).append(CheckUtils.appendSymbol(value))
.append(space).append(") ");
}
if (condition.equals(SearchConditionUtil.NOT_IN)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append("(")
.append(space).append(CheckUtils.appendSymbol(value))
.append(space).append(") ");
}
if (condition.equals(SearchConditionUtil.LIKE)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append("'%").append(value).append("%'").
append(space);
}
if (condition.equals(SearchConditionUtil.NOT_LIKE)) {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append("'%").append(value).append("%'").
append(space);
}
if (condition.equals(SearchConditionUtil.BETWEEN)) {
String[] contentUnits = value.split(",");
if (contentUnits.length != 2) {
throw new RuntimeException("搜索值栏位必须输入两个参数且用“,”隔开");
}
String leftContentUnit = contentUnits[0];
String rightContentUnit = contentUnits[1];
if(CheckUtils.isNumber(leftContentUnit) == false){
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition).append(space)
.append("'").append(leftContentUnit).append("'")
.append(space).append("and").append(space)
.append("'").append(rightContentUnit).append("' ");
}else {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(leftContentUnit)
.append(space).append("and")
.append(space).append(rightContentUnit).
append(space);
}
}
if (condition.equals(SearchConditionUtil.NOT_BETWEEN)) {
String[] contentUnits = value.split(",");
if (contentUnits.length != 2) {
throw new RuntimeException("搜索值栏位必须输入两个参数且用“,”隔开");
}
String leftContentUnit = contentUnits[0];
String rightContentUnit = contentUnits[1];
if(CheckUtils.isNumber(leftContentUnit) == false){
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition).append(space)
.append("'").append(leftContentUnit).append("'")
.append(space).append("and").append(space)
.append("'").append(rightContentUnit).append("' ");
}else {
searchParamer.append(space).append(relation)
.append(space).append(field)
.append(space).append(condition)
.append(space).append(leftContentUnit)
.append(space).append("and")
.append(space).append(rightContentUnit).
append(space);
}
}
}
if(!CollectionUtils.isEmpty(searchConditionList) && StringUtils.isNotBlank(searchParamer)) {
searchParamer.insert(4, "(");
searchParamer.append(")");
}
return searchParamer;
}
}
3.前端的拦截器的工具类
http.interceptors.response.use(response => {
if (response.data && response.data.code === 401) {
clearLoginInfo()
localStorage.clear()
sessionStorage.clear()
window.location.href = constant.loginUrl
};
if (response.data && response.data.resultCode == 500) {
Message({
message: response.data.resultMsg || response.data.message,
type: 'warning',
showClose: true
})
return response.data
} else {
return response.data
}
}, error => {
console.log('error.message', error.message)
console.log('error', error)
if (error.message == 'Request failed with status code 404') {
Message({
message: '404 Not Fount,请刷新页面或者请联系管理员!',
type: 'warning',
showClose: true
})
}
if (error.message == 'Request failed with status code 500') {
Message({
message: error.response.data.resultMsg,
type: 'warning',
showClose: true
})
}
if (error) {
switch (error.message) {
case 'Request failed with status code 401' :
store.dispatch('LogOut', {})
if (flag) {
alert('您登录已失效,请重新登录')
flag = false
setTimeout(() => {
flag = true
}, 3000)
}
clearLoginInfo()
localStorage.clear()
sessionStorage.clear()
window.sessionStorage.setItem('tagActives', '')
window.location.href = constant.loginUrl
break
}
}
return Promise.reject(error)
})