GlobalExceptionHandler.java 887 B

12345678910111213141516171819202122232425262728293031
  1. package com.izouma.walkchina.exception;
  2. import com.izouma.walkchina.bean.Result;
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import javax.servlet.http.HttpServletRequest;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. @ControllerAdvice
  10. public class GlobalExceptionHandler {
  11. @ExceptionHandler(value = ServiceException.class)
  12. @ResponseBody
  13. public Result serviceExceptionHandler(HttpServletRequest req, ServiceException e) {
  14. Map<String, Object> map = new HashMap<>();
  15. map.put("url", req.getRequestURL().toString());
  16. return Result.builder()
  17. .success(false)
  18. .error(e.getMessage())
  19. .data(map)
  20. .code(-1)
  21. .build();
  22. }
  23. }