函数式接口@FunctionalInterface

 PIG: 函数式接口让你的代码更优雅和复用性更高

package com.dj.springtest.service;

import javax.servlet.http.HttpServletResponse;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 1:26
 * Description: 有且只有一个抽象方法,但可以定义多个 default 或 static 方法
 */
@FunctionalInterface
public interface ExceptionFunction {

    void throwException(Integer httpStatus, String errMessage, HttpServletResponse response);
}

 异常信息枚举类

package com.dj.springtest.constant;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 2:25
 * Description: No Description
 */
public enum ApiErrorEnum {

    UNKNOWN_SYSTEM_EXCEPTION(10500,"系统发生未知异常!");

    private Integer code;
    private String message;

    ApiErrorEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

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

}

 lamdba表达式实现函数式接口,达到封装一个异常处理的逻辑

package com.dj.springtest.utils;

import com.dj.springtest.service.ExceptionFunction;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 1:45
 * Description: 如果为true,就抛异常,否则不处理
 */
public class ExceptionUtil {

    //返回接口ExceptionFunction的匿名内部类,只使用一次哦!
    public static ExceptionFunction isTrue(Boolean flag) {

        //常规写法
        /*
        return new ExceptionFunction() {
            @Override
            public void throwException(Integer httpStatus, String errMessage, HttpServletResponse response) {
                if(flag){
                    response.setStatus(httpStatus);
                    response.setCharacterEncoding(String.valueOf(StandardCharsets.UTF_8));
                    throw new RuntimeException(errMessage);
                }
            }
        };
        */

        //lambda表达式
        return (status, errMessage, response) -> {
            if(flag){
                response.setStatus(status);
                response.setCharacterEncoding(String.valueOf(StandardCharsets.UTF_8));
                throw new RuntimeException(errMessage);
            }
        };
    }
}

测试使用案例 

package com.dj.springtest.demo;

import com.dj.springtest.constant.ApiErrorEnum;
import com.dj.springtest.utils.ExceptionUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;

import javax.servlet.http.HttpServletResponse;

/**
 * User: ldj
 * Date: 2023/10/15
 * Time: 2:15
 * Description:函数式接口让你的代码更优雅和复用性更高
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExceptionUtilTest {

    @Autowired
    private HttpServletResponse response;

    @Test
    public void testExceptionFunction() {
        //模拟入参
        boolean flag1 = false;
        boolean flag2 = true;

        //1.先看常规写法
        if (flag1) {
            throw new RuntimeException("获取用户信息出错!");
        } else {
            System.out.println("继续执行业务代码...");
        }

        //2.函数式写法
        ExceptionUtil.isTrue(flag2).throwException(HttpStatus.INTERNAL_SERVER_ERROR.value(), ApiErrorEnum.UNKNOWN_SYSTEM_EXCEPTION.getMessage(), response);
    }
}

函数式接口@FunctionalInterface_第1张图片

你可能感兴趣的:(java,java,函数式接口)