SpringBootユニファイドインターフェイスの戻りとグローバル例外処理

一緒に書く習慣を身につけましょう!「ナゲッツデイリーニュープラン・4月アップデートチャレンジ」に参加して12日目です。クリックしてイベントの詳細をご覧ください。

序文

少し前に古いプロジェクトを引き継いだので、このプロジェクトに新しい要件を追加する必要があります。同僚が開発プロセス中に問題に遭遇しましたか?

1.成功のステータスが200であろうと0であろうと、注文システム200は成功を表し、メンバーシップシステム0は成功を表します。

2.インターフェースから返される結果の中には、msgフィールドで記述されているものと、descフィールドで記述されているものがあります。前の部分を処理するのは面倒です。統合できますか?

3.エラーメッセージは国際化をサポートする必要があります。

実際、これらの問題は、最終的な分析では、コード仕様の問題です。インターフェイス定義とグローバル例外を均一に処理する必要があります。履歴プロジェクトには10​​を超えるプロジェクトがあります。各プロジェクトを一度に実装することは可能ですか?答えは絶対に不可能です。

解決

picture.png

パブリックモジュールを定義し、統一されたインターフェイス定義仕様と例外処理を実装します。他のシステムはこれに依存して拡張できます。

実装

2.1ステータスコードの統一されたインターフェースの定義

public interface BaseResultCode
{
    /**
     * 状态码
     * @return
     */
    int getCode();
    
    /**
     * 提示信息
     * @return
     */
    String getMsg();
}

复制代码

2.2パブリックモジュールのステータスコード列挙型クラス

public enum ResultCode implements BaseResultCode
{
    OK(200, "成功"),
    ERROR(300,"系统异常"), 
    NEED_AUTH(301, "非法请求,请重新登录"), 
    PARAMTER_ERROR(302, "参数错误");
    //省略其他定义错误码
   
    private int code;

    private String msg;

    private ResultCode(int code, String msg)
    {
        this.code = code;
        this.msg = msg;
    }

    public static ResultCode getValue(int code)
    {
        for (ResultCode errorCode : values())
        {
            if (errorCode.getCode() == code)
            {
                return errorCode;
            }
        }
        return null;
    }
   //省略Get、Set方法
 }

复制代码

2.3グローバルカスタム例外を定義する

public class SysException extends RuntimeException
{
    private static final long serialVersionUID = 5225171867523879342L;
    
    private int code;
    
    private String msg;
    
    private Object[] params;

    private BaseResultCode errorCode;

    public SysException()
    {
        super();
    }
    
    public SysException(String message)
    {
        super(message);
    }

    public SysException(Throwable cause)
    {
        super(cause);
    }
    
    public SysException(int code ,String message)
    {
        this.code = code;
        this.msg = message;
    }

    public SysException(int code ,String message,  Object[] params)
    {
        this(code, message);
        this.params= params;
    }
    
    public SysException(String message, Throwable cause)
    {
        super(message, cause);
    }
    
    public SysException(BaseResultCode errorCode)
    {
        this.errorCode = errorCode;
    }

    public SysException(String message, Object[] params)
    {
        super(message);
        this.params = params;
    }
    
    public SysException(BaseResultCode errorCode, String message, Object[] params)
    {
        this(message, params);
        this.errorCode = errorCode;
    }
 
    

    /**
     * Construct by default
     * 
     * @param message
     *            message
     * @param parameters
     *            parameters
     * @param cause
     *            cause
     */
    public SysException(String message, Object[] params, Throwable cause)
    {
        super(message, cause);
        this.params = params;
    }
    

    public int getCode()
    {
        return code;
    }

    public void setCode(int code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    /**
     * @return the params
     */
    public Object[] getParams()
    {
        return params;
    }

    /**
     * @param params
     *            the params to set
     */
    public void setParams(Object[] params)
    {
        this.params = params;
    }

    public BaseResultCode getErrorCode()
    {
        return errorCode;
    }

    public void setErrorCode(BaseResultCode errorCode)
    {
        this.errorCode = errorCode;
    }
    
    
}
复制代码

2.3統一されたインターフェース形式の出力クラスの定義

public class Result implements Serializable
{
    private static final long serialVersionUID = -1773941471021475043L;

    private Object data;

    private int code;

    private String msg;

    public Result()
    {
    }

    public Result(int code, Object data, String msg)
    {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public Result(int code, String desc)
    {
        this(code, null, desc);
    }

    public Result(BaseResultCode errorCode)
    {
        this(errorCode.getCode(), null, errorCode.getMsg());
    }

    public static Result success()
    {
        return success(null);
    }

    public static Result success(Object data)
    {
        Result result = new Result();
        result.setData(data);
        result.setCode(ResultCode.OK.getCode());
        return result;
    }

    public static Result error(String msg)
    {
        Result result = new Result();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMsg(msg);
        return result;
    }
    
    public static Result error(BaseResultCode baseCode)
    {
        Result result = new Result();
        result.setCode(baseCode.getCode());
        result.setMsg(baseCode.getMsg());
        return result;
    }
}
复制代码

個人的な提案:統合インターフェース出力クラスはジェネリック型として定義されるべきではありません

2.4統一されたインターフェース形式の出力クラスを定義する

@RestControllerAdvice
public class SysExceptionHandler
{
    public static Log logger = LogManager.getLogger(SysExceptionHandler.class);
    
    @ExceptionHandler(Exception.class)
    public Result handleException(HttpServletRequest request,
            Exception ex)
    {
        logger.error("Handle Exception Request Url:{},Exception:{}",request.getRequestURL(),ex);
        Result result = new Result();
        //系统异常
        if (ex instanceof SysException)
        {
            SysException se = (SysException) ex;
            BaseResultCode resultCode =se.getErrorCode();
            if(resultCode==null)
            {
                result = Result.error(se.getMessage());
            }
            else
            {
               result = new Result(resultCode.getCode(),
                                       StringUtil.isNotEmpty(se.getMessage())?se.getMessage():resultCode.getMsg());
            }
        }
        //参数错误
        else if (ex instanceof ConstraintViolationException)
        {
            ConstraintViolationException v = (ConstraintViolationException) ex;
            String message = v.getConstraintViolations().iterator().next()
                    .getMessage();
            result.setCode(ResultCode.PARAMTER_ERROR.getCode());
            result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message);
        }
        //参数错误
        else if (ex instanceof BindException)
        {
            BindException v = (BindException) ex;
            String message = v.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
            result.setCode(ResultCode.PARAMTER_ERROR.getCode());
            result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message);
        }
        //参数错误
        else if (ex instanceof MethodArgumentNotValidException)
        {
            MethodArgumentNotValidException v = (MethodArgumentNotValidException) ex;
            String message = v.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
            result.setCode(ResultCode.PARAMTER_ERROR.getCode());
            result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message);
        }
        else
        {
           result = new Result(ResultCode.ERROR.getCode(),ExceptionUtil.getErrorMsg(ex));
        }
        logger.info("exception handle reuslt:" + result);
        return result;
    }
}
复制代码

上記の定義は、グローバルインターフェイスと例外の統合処理をすでに実現できますが、次の問題が存在します。

各コントローラーは結果タイプを返す必要があり、各メソッドはResult.success()またはResult.success(data)の結果を返す必要があります。これは少し反復的であり、最適化する必要があります。

    @GetMapping("addUser")
    public Result add()
    {
       for(int i=0;i<10;i++)
       {
           TUser user = new TUser();
           //user.setOid(IdWorker.getId());
           user.setName("shareing_"+i);
           user.setAge(i);
           userService.addUser(user);
       }
       return Result.success();
    }
复制代码

2.5インターフェース統合出力の最適化

実装メソッドは、ResponseBodyAdviceインターフェースを実装し、beforeBodyWriteメソッドインターフェースをオーバーライドするだけで済みます。

@RestControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object>
{
    private Logger logger = LoggerFactory.getLogger(ResponseAdvice.class);
    
    @Override
    public boolean supports(MethodParameter returnType,
            Class<? extends HttpMessageConverter<?>> converterType)
    {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter returnType,
            MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request, ServerHttpResponse response)
    {
        logger.info("before body write param:{}",o);
        
        if(o instanceof String)
        {
           //序列化结果输出
           return FastJsonUtil.toJSONString(Result.success(o));
        }
        else if (o instanceof Result)
        {
             return o;
        }
        return Result.success(o);
    }
}
复制代码

最適化後、コントローラー出力はビジネス要件に従って出力オブジェクトを定義できます。

 @GetMapping("getUserByName")
    public TUser getUserByName1(@RequestParam String name)
    {
       logger.info("getUserByName paramter name:"+name);
       return userService.getUserByName(name); 
    }
复制代码

2.6サブシステムの実装方法

1.サブシステムは、共通のjarパッケージを導入します。

  <dependency>
	    <groupId>com.xx</groupId>
	    <artifactId>xx-common</artifactId>
	    <version>2.0</version>
	</dependency>
复制代码

2.サブシステムは、ステータスコードを定義し、BaseResultCodeインターフェイスを実装します

 public enum OrderModelErrorCode implements BaseResultCode
{
    ORDER_STATUS_ERROR(1000, "订单状态不正确");
    
    private int code;

    private String msg;
    
    private UserModelErrorCode(int code, String msg)
    {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public int getCode()
    {
        return code;
    }

    @Override
    public String getMsg()
    {
        return msg;
    }
}
复制代码

3.例外処理クラスを定義し、パブリック例外処理クラスSysExceptionHandlerを継承します

@RestControllerAdvice
public class OrderModalExceptionHandle extends SysExceptionHandler
{
     @Override
    public Result handleException(HttpServletRequest request, Exception ex)
    {
        return super.handleException(request, ex);
        //子系统可以扩展异常处理
        
    }
}
复制代码

4.サブシステムの使用例

@Override
public Order getOrder(String orderId)
{
	Order order =getOrder(orderId);
        //相关伪代码
	if(order.getStatus()>120)
	{
	   throw new SysException(OrderModelErrorCode.ORDER_STATUS_ERROR);    
	}
	return order;
}
复制代码

関連プロジェクトの再構築後、第1と第2の問題は解決されました。第3の国際化問題については、後続の記事で説明します。

要約する

プロジェクトの開発においては、プロジェクト開発マニュアルは依然として非常に重要です。たとえば、<<アリババJava開発マニュアル>>
には、実際の戦闘やピットを踏むことで前任者がまとめた多くの重要な仕様が記載されています。前任者のようになりましょう。間違いをしないように学びましょう。

おすすめ

転載: juejin.im/post/7086031796755922981