springboot custom exception

Spring boot custom exception and exception handling In a web project, we may need to return different prompt codes to the front end. For example, 401 ...
I. user defined exception class
II. User defined exception handling
3. Custom return
IV. test output
V. output results
Spring boot custom exception and exception handling

In a web project, we may need to return different prompt codes to the front end. For example, 401 indicates no permission, 500 indicates abnormal location, 200 indicates successful request, etc. However, these prompt codes are far from enough to meet the prompt we return to the front end. We may also need to customize the error codes to the front end. The front end obtains the corresponding error codes and error information and displays them on the page.

Using custom exception can solve these return values. Using custom exception and exception handling, we can customize our return code and error information when returning.

I. user defined exception class

/** * @author: lxw * @Date: 2019/2/16 20:00 * @email: * @Description: Custom exception (inherit runtime exception) */ public class ExceptionUtils extends RuntimeException { private static final long serialVersionUID = 1L; /** * Error coding */ private int code; /** * Whether the message is the Key in the property file */ private boolean propertiesKey = true; /** * Construct a basic exception * * @param message Information description */ public ExceptionUtils(String message) { super(message); } /** * Construct a basic exception * * @param code Error coding * @param message Information description */ public ExceptionUtils(int code, String message) { this(code, message, true); } /** * Construct a basic exception * * @param code Error coding * @param message Information description */ public ExceptionUtils(int code, String message, Throwable cause) { this(code, message, cause, true); } /** * Construct a basic exception * * @param code Error coding * @param message Information description * @param propertiesKey Whether the message is the Key in the property file */ public ExceptionUtils(int code, String message, boolean propertiesKey) { super(message); this.setCode(code); this.setPropertiesKey(propertiesKey); } /** * Construct a basic exception * * @param code Error coding * @param message Information description */ public ExceptionUtils(int code, String message, Throwable cause, boolean propertiesKey) { super(message, cause); this.setCode(code); this.setPropertiesKey(propertiesKey); } /** * Construct a basic exception * * @param message Information description * @param cause Root exception class (any exception can be stored) */ public ExceptionUtils(String message, Throwable cause) { super(message, cause); } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public boolean isPropertiesKey() { return propertiesKey; } public void setPropertiesKey(boolean propertiesKey) { this.propertiesKey = propertiesKey; } }

II. User defined exception handling

import com.modules.common.utils.RUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DuplicateKeyException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.NoHandlerFoundException; /** * @author: lxw * @Date: 2019/2/16 20:00 * @email: * @Description: Custom exception handling */ @RestControllerAdvice public class RExceptionUtilsHandler { private Logger logger = LoggerFactory.getLogger(getClass()); /** * Handling custom exceptions */ @ExceptionHandler(ExceptionUtils.class) public RUtils handleRRException(ExceptionUtils e) { RUtils r = new RUtils(); r.put("code", e.getCode()); r.put("msg", e.getMessage()); return r; } /** * Path exception handling not found */ @ExceptionHandler(NoHandlerFoundException.class) public RUtils handlerNoFoundException(Exception e) { logger.error(e.getMessage(), e); return RUtils.error(404, "The path does not exist, please check whether the path is correct"); } /** * Database exception handling */ @ExceptionHandler(DuplicateKeyException.class) public RUtils handleDuplicateKeyException(DuplicateKeyException e) { logger.error(e.getMessage(), e); return RUtils.error("The record already exists in the database"); } /** * General exception handling */ @ExceptionHandler(Exception.class) public RUtils handleException(Exception e) { logger.error(e.getMessage(), e); return RUtils.error(); } }

3. Custom return

package com.modules.common.utils; import java.util.HashMap; import java.util.Map; /** * @author: lxw * @Date: 2019/2/19 11:19 * @email: * @Description: Custom return value */ public class RUtils extends HashMap<String, Object> { private static final long serialVersionUID = 1L; /** * Return normally by default. You can use new RUtils() to return */ public RUtils() { put("code", 0); } /** * Expressing exceptions */ public static RUtils error() { return error(500, "Unknown exception, please contact administrator"); } public static RUtils error(String msg) { return error(500, msg); } /** * Custom exception error code */ public static RUtils error(int code, String msg) { RUtils r = new RUtils(); r.put("code", code); r.put("msg", msg); return r; } /** * Normal return with information */ public static RUtils ok(String msg) { RUtils r = new RUtils(); r.put("msg", msg); return r; } public static RUtils ok(Map<String, Object> map) { RUtils r = new RUtils(); r.putAll(map); return r; } public static RUtils ok() { return new RUtils(); } @Override public RUtils put(String key, Object value) { super.put(key, value); return this; } }

IV. test output

/** * @author: lxw * @Date: 2018/10/19 19:36 * @email: [email protected] * @Description: Test file */ @RestController @RequestMapping("/") public class TestController { /** * Test custom exception * * @return RUtils */ @ApiOperation(value = "Test custom exception", notes = "Test custom exception") @GetMapping(value = "/exceptionTest") public RUtils exceptionTest() { String msg = new ExceptionUtils(500, "Test exception").getMessage(); int errorCode = new ExceptionUtils(500, "Test exception").getCode(); return RUtils.error(errorCode, msg); } }

V. output results

{"msg":"Test exception","code":500}

3 December 2019, 00:23 | Views: 8180

Add new comment

For adding a comment, please log in
or create account

0 comments