package com.izouma.walkchina.exception; import com.izouma.walkchina.bean.Result; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = ServiceException.class) @ResponseBody public Result serviceExceptionHandler(HttpServletRequest req, ServiceException e) { Map map = new HashMap<>(); map.put("url", req.getRequestURL().toString()); return Result.builder() .success(false) .error(e.getMessage()) .data(map) .code(-1) .build(); } }