LogJsonResultHolder.java 572 B

1234567891011121314151617181920212223242526272829303132
  1. package com.jeesite.modules.sys.log;
  2. /**
  3. * 当前请求的 JSON 返回体,供访问日志落库使用。
  4. */
  5. public final class LogJsonResultHolder {
  6. private static final ThreadLocal<String> HOLDER = new ThreadLocal<>();
  7. private LogJsonResultHolder() {
  8. }
  9. public static void set(String json) {
  10. if (json == null || json.isEmpty()) {
  11. HOLDER.remove();
  12. } else {
  13. HOLDER.set(json);
  14. }
  15. }
  16. public static String getAndRemove() {
  17. try {
  18. return HOLDER.get();
  19. } finally {
  20. HOLDER.remove();
  21. }
  22. }
  23. public static void remove() {
  24. HOLDER.remove();
  25. }
  26. }