前言:
此刻大家对“centos启动no controller found”大约比较讲究,小伙伴们都需要了解一些“centos启动no controller found”的相关内容。那么小编也在网络上网罗了一些对于“centos启动no controller found””的相关资讯,希望你们能喜欢,兄弟们快快来了解一下吧!说明:
SpringBoot版本:2.3.3.RELEASE
采用,注解@ControllerAdvice方式和application.yml中配置的方式,全局统一处理@Controller、@RestController以及容器的异常;
代码:
package com.what21.demo.common;import lombok.Data;/** * 定义运行时异常处理 */@Datapublic class BusinessException extends RuntimeException { private Integer code; private String message; public BusinessException(String message) { super(message); } public BusinessException(String message, Throwable cause) { super(message, cause); } public BusinessException(Throwable cause) { super(cause); } /** * @param code * @param message */ public BusinessException(Integer code, String message) { super(code + ":" + message); this.code = code; this.message = message; }}
package com.what21.demo.common;import lombok.*;import lombok.experimental.Accessors;import java.io.Serializable;@ToString@NoArgsConstructor@AllArgsConstructor@Accessors(chain = true)public class R<T> implements Serializable { private static final long serialVersionUID = 1L; public static final String OK = "SUCCESS"; public static final String ERROR = "ERROR"; @Getter @Setter private String code; @Getter @Setter private String msg; @Getter @Setter private T data; public static <T> R<T> ok() { return instance(OK, null, null); } public static <T> R<T> ok(T data) { return instance(OK, null, data); } public static <T> R<T> ok(T data, String msg) { return instance(OK, msg, data); } public static <T> R<T> okMsg(String msg) { return instance(OK, msg, null); } public static <T> R<T> error() { return instance(ERROR, null, null); } public static <T> R<T> error(T data) { return instance(ERROR, null, data); } public static <T> R<T> error(T data, String msg) { return instance(ERROR, msg, data); } public static <T> R<T> errMsg(String msg) { return instance(ERROR, msg, null); } public static <T> R<T> instance(String code, String msg, T data) { R<T> result = new R<>(); result.setCode(code); result.setMsg(msg); result.setData(data); return result; }}
package com.what21.demo.common;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.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestControllerAdvice;import org.springframework.web.servlet.NoHandlerFoundException;import java.util.List;@ControllerAdvice// @RestControllerAdvice@Slf4jpublic class GlobalExceptionHandler { /** * 自定义异常处理 */ @ExceptionHandler(value = BusinessException.class) @ResponseBody public R businessExceptionHandler(BusinessException e) { return R.instance(e.getCode() + "", e.getMessage(), ""); } /** * 异常处理 */ @ExceptionHandler(value = Exception.class) @ResponseBody public R exceptionHandler(Exception e) { if (e instanceof NoHandlerFoundException) { return R.instance("1001", e.getMessage(), ""); } else if (e instanceof IllegalArgumentException) { return R.instance("1001", e.getMessage(), ""); } else if (e instanceof IllegalStateException) { return R.instance("1001", e.getMessage(), ""); } else if (e instanceof BindException) { BindException ex = (BindException) e; List<ObjectError> allErrors = ex.getAllErrors(); ObjectError error = allErrors.get(0); String defaultMessage = error.getDefaultMessage(); return R.instance("1001", defaultMessage, ""); } else if (e instanceof MethodArgumentNotValidException) { MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e; List<ObjectError> errors = ex.getBindingResult().getAllErrors(); String message = errors.get(0).getDefaultMessage(); return R.instance("1001", message, ""); } return R.instance("1001", e.getMessage(), ""); }}
配置(application.yml):
spring: #[自定义404]:出现错误时, 直接抛出异常 mvc: throw-exception-if-no-handler-found: true #[自定义404]:不映射工程中的资源文件 resources: add-mappings: false
模拟错误:
import com.what21.demo.common.BusinessException;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class EchoController { @GetMapping(value = "/") public String index(Model model) { model.addAttribute("index", "首页"); return "index"; } @RequestMapping(path = "/echo", method = {RequestMethod.GET}) @ResponseBody public String echo() { if (true) { throw new BusinessException(1002, "异常..........."); } return "hello"; }}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。