最开始的判断和返值:
if(StringUtils.isEmpty(ybsscjModel.getUserId())){
result.setStatus("-1");
result.setErrorCode("201");
result.setErrorMessage("用户编号不能为空");
return result;
}
if(StringUtils.isEmpty(ybsscjModel.getUserName())){
result.setStatus("-1");
result.setErrorCode("201");
result.setErrorMessage("用户名不能为空");
return result;
}
......
每次每个判断都需要写重复的4行,代码累赘且繁复。
下面通过自定义异常,抛异常,捕捉异常,静态重载方法等使重复代码简化。
并继承RuntimeException,如果需要继承其他的异常,可再写一个自定义异常类。
package com.chen.exception;
/**
* 自定义异常
* @author chen
*
*/
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
if(StringUtils.isEmpty(ybsscjModel.getUserId())){
throw new CustomException("用户编号不能为空");
}
if(StringUtils.isEmpty(ybsscjModel.getUserName())){
throw new CustomException("用户名不能为空");
}
@ExceptionHandler(CustomException.class)
@ResponseBody
private Result exceptionHandler(Exception e){
return ResultUtils.error(e.getMessage());
}
/*
@ExceptionHandler()里可以写多个异常类,如@ExceptionHandler(E1.class,E2.class,E3.class...)
*/
package com.chen.controller.intelCommunity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chen.exception.CustomException;
import com.chen.model.api.Result;
import com.chen.util.ResultUtils;
/**
* 全局异常处理
* @author chen
*
*/
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(CustomException.class)
@ResponseBody
private Result exceptionHandler(Exception e){
return ResultUtils.error(e.getMessage());
}
}
两个方式的所写的方法是一致的,并无差别。
抛了异常之后,返回值依然需要设置,这里就可以通过静态方法重载来实现。
package com.chen.util;
import com.chen.model.api.Result;
/**
* Result工具类
* @author chen
*
*/
public class ResultUtils {
public static Result success(){
Result result = new Result();
result.setStatus("0");
result.setErrorMessage("成功");
return result;
}
public static Result success(String message){
Result result = new Result();
result.setStatus("0");
result.setErrorMessage(message);
return result;
}
public static Result success(String status, String message){
Result result = new Result();
result.setStatus(status);
result.setErrorMessage(message);
return result;
}
public static Result error(){
Result result = new Result();
result.setStatus("-1");
result.setErrorCode("205");
result.setErrorMessage("系统异常");
return result;
}
public static Result error(String message){
Result result = new Result();
result.setStatus("-1");
result.setErrorCode("201");
result.setErrorMessage(message);
return result;
}
public static Result error(String errorCode, String message){
Result result = new Result();
result.setStatus("-1");
result.setErrorCode(errorCode);
result.setErrorMessage(message);
return result;
}
public static Result error(String status, String errorCode, String message){
Result result = new Result();
result.setStatus(status);
result.setErrorCode(errorCode);
result.setErrorMessage(message);
return result;
}
}
最后,将打印结果展示:
//失败之一
{
"status": "-1",
"content": null,
"type": 0,
"seq": 1530854856476,
"errorCode": "201",
"errorMessage": "用户编号不能为空"
}
//成功
{
"status": "0",
"content": "推送成功",
"type": 0,
"seq": 1530855706892,
"errorCode": null,
"errorMessage": null
}
附件: result的结果实体类
package com.chen.model.app;
public class Result {
private static final long serialVersionUID = 1L;
private String status;//0为成功,1为错误
private String content;//信息
public int type;//标示符
public Long seq;//时间戳
public String errorCode;//错误码
public String errorMessage;//错误提示
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public Result() {
seq = System.currentTimeMillis();
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Long getSeq() {
return seq;
}
public void setSeq(Long seq) {
this.seq = seq;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}