1.全局异常处理类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.yidong.exceptions.LoginException;
import com.yidong.pojo.HttpResult;
@RestControllerAdvice
public class GlobalExceptionHandler
{
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
public HttpResult loginException(Exception e)
{
log.error(e.getMessage(), e);
return HttpResult.error(e.getMessage());
}
}
2.controller方法throws Exception
@RequestMapping("/test/index")
@SuppressWarnings("unchecked")
public Map<String, Object> index(@RequestBody Map<String,Object> map) throws Exception {
return HttpResult.success("登录成功!",repMap);
}