SpringCloud-技术专区-Gateway优雅的处理Filter抛出的异常

导读:本篇文章讲解 SpringCloud-技术专区-Gateway优雅的处理Filter抛出的异常,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

 

  

涉及到的工具类如下:

JsonHelper

public class JsonHelper {

    /**
     * Json美化输出工具
     * @param json json string
     * @return beauty json string
     */
    public static String toPrettyFormat(String json) {
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(jsonObject);
    }

    /**
     * Json转换成对象
     * @param json json string
     * @param <T> 对象类型
     * @return T
     */
    public static  <T> T jsonToObject(String json,Class<T> t){
        Gson gson = new Gson();
        return gson.fromJson(json,t);
    }

    /**
     * 对象转换成Json字符串
     * @param object obj
     * @return json string
     */
    public static String objectToJson(Object object){
        Gson gson = new Gson();
        return gson.toJson(object);
    }

}

  

SpringContextHolder

@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertApplicationContext();
        return applicationContext.getBean(requiredType);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        assertApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }

    /**
     * 通过类上的注解获取类
     *
     * @param annotation anno
     * @return map
     */
    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotation) {
        assertApplicationContext();
        return applicationContext.getBeansWithAnnotation(annotation);
    }

    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("application Context属性为null,请检查是否注入了SpringContextHolder!");
        }
    }

}

  

测试

编写异常处理类

@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {

    @ExceptionHandler(Throwable.class)
    public xxx handler(Throwable e){
        return xxx;
    }
}

  

  

涉及到的工具类如下:

JsonHelper

public class JsonHelper {

    /**
     * Json美化输出工具
     * @param json json string
     * @return beauty json string
     */
    public static String toPrettyFormat(String json) {
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(jsonObject);
    }

    /**
     * Json转换成对象
     * @param json json string
     * @param <T> 对象类型
     * @return T
     */
    public static  <T> T jsonToObject(String json,Class<T> t){
        Gson gson = new Gson();
        return gson.fromJson(json,t);
    }

    /**
     * 对象转换成Json字符串
     * @param object obj
     * @return json string
     */
    public static String objectToJson(Object object){
        Gson gson = new Gson();
        return gson.toJson(object);
    }

}

  

SpringContextHolder

@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHolder.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType) {
        assertApplicationContext();
        return applicationContext.getBean(requiredType);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        assertApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }

    /**
     * 通过类上的注解获取类
     *
     * @param annotation anno
     * @return map
     */
    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotation) {
        assertApplicationContext();
        return applicationContext.getBeansWithAnnotation(annotation);
    }

    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("application Context属性为null,请检查是否注入了SpringContextHolder!");
        }
    }

}

  

测试

编写异常处理类

@RestControllerAdvice
public class GlobalExceptionHandlerAdvice {

    @ExceptionHandler(Throwable.class)
    public xxx handler(Throwable e){
        return xxx;
    }
}

  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/70193.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!