canvas网站源码,西安企业排名,重庆网站建设,无极在线招聘网最新招聘1.修改application.properties文件# 自定义404#出现错误时, 直接抛出异常spring.mvc.throw-exception-if-no-handler-foundtrue#不要为我们工程中的资源文件建立映射spring.resources.add-mappingsfalse2.添加controller增强处理if (e instanceof NoHandlerFoundException) {re…1.修改application.properties文件# 自定义404#出现错误时, 直接抛出异常spring.mvc.throw-exception-if-no-handler-foundtrue#不要为我们工程中的资源文件建立映射spring.resources.add-mappingsfalse2.添加controller增强处理if (e instanceof NoHandlerFoundException) {return ResultUtil.error(ResultEnum.NO_HANDLER_FOUND_ERROR);}3.测试访问 http://localhost:8080/hello1// 20190705114619// http://localhost:8080/hello1{code: 404,msg: 接口不存在,data: null}4.完整controller增强处理类package com.kevin.common.exception;import com.kevin.common.entity.Result;import com.kevin.common.enums.ResultEnum;import com.kevin.common.util.ResultUtil;import lombok.extern.slf4j.Slf4j;import org.springframework.validation.BindException;import org.springframework.validation.ObjectError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestControllerAdvice;import org.springframework.web.servlet.NoHandlerFoundException;import java.util.List;/*** 异常处理器** author kevin* date 2019/7/4 14:46*/RestControllerAdviceSlf4jpublic class KevinExceptionHandler {ExceptionHandler(Exception.class)public Result handleException(Exception e) {log.error(e.getMessage(), e);if (e instanceof KevinException) {return ResultUtil.error(e.getMessage());} else if (e instanceof NoHandlerFoundException) {return ResultUtil.error(ResultEnum.NO_HANDLER_FOUND_ERROR);} else if (e instanceof IllegalArgumentException) {return ResultUtil.error(e.getMessage());} else if (e instanceof IllegalStateException) {return ResultUtil.error(e.getMessage());} else if (e instanceof BindException) {BindException ex (BindException) e;List allErrors ex.getAllErrors();ObjectError error allErrors.get(0);String defaultMessage error.getDefaultMessage();return ResultUtil.error(defaultMessage);} else if (e instanceof MethodArgumentNotValidException) {MethodArgumentNotValidException ex (MethodArgumentNotValidException) e;List errors ex.getBindingResult().getAllErrors();String message errors.get(0).getDefaultMessage();return ResultUtil.error(message);} else {return ResultUtil.error(ResultEnum.UNKNOW_ERROR);}}}好了玩的开心最近在整合一个springboot2.X的框架。里面就集成了这块有兴趣的可以下载下来看看地址https://github.com/FunCodingOfWe/kevin-boot欢迎start