涉及到的工具类如下:
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.cnblogs.com/liboware/p/13225874.html,任何足够先进的科技,都与魔法无异。
涉及到的工具类如下:
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.cnblogs.com/liboware/p/13225874.html,任何足够先进的科技,都与魔法无异。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/70193.html