First, the front and back end call relationship
In general, the front and back end calls are as follows:
Unified exception handling
When all client calls must pass through Controller, Controller can be intercepted for processing, with the following class structure:
Reference code is as follows: 1. GlobalExceptionHandler. Abnormal Java interceptor is returned to the client for processing.
import com.javashizhan.domain.Response;
import com.javashizhan.exception.ThrowableHandler;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
/ * * *@ClassName GlobalExceptionHandler
* @DescriptionGlobal exception handling, catching all exceptions thrown by Controller,@ResponseBodyComments must be added, otherwise they jump to the default error page *@AuthorHis burning a leaf *@Date2019/9/28 comest *@Version1.0 * * * / javashizhan.com
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
/** * by default, httpstatus. OK is returned. This is not required@param e
* @returnThe encapsulated reply object */
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler({Throwable.class})
public Response handle(Throwable e) {
returnThrowableHandler.handle(e); }}Copy the code
2. Throwablehandler. Java handles exceptions decoupled from GlobalExceptionHandler so that non-Controller can be reused as a call entry class.
import java.io.PrintWriter;
import java.io.StringWriter;
import com.javashizhan.domain.Response;
/ * * *@ClassName ThrowableHandler
* @DescriptionException handling class that converts an exception into a unified reply object *@AuthorSonorous leaf *@Date 2019/9/28 23:30
* @Version1.0 * * * / javashizhan.com
public class ThrowableHandler {
private ThrowableHandler(a) {}
public static Response handle(Throwable t) {
Response response = new Response();
response.setStatus(Response.STAUTS_FAILED);
response.setErrorMsg(t.getMessage());
response.setErrorStack(getErrorStack(t));
return response;
}
private static String getErrorStack(Throwable t) {
if (null! = t) { PrintWriter pw =null;
StringWriter sw = new StringWriter();
pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
}
return ""; }}Copy the code
3.Response. Java exception conversion results, regardless of whether there is an exception, this class can be used.
/ * * *@ClassName A
* @Description TODO
* @AuthorSonorous leaf *@Date2019/9/28 comest *@Version1.0 * * * / javashizhan.com
public final class Response<T> {
/** Reply status - Success */
public static final int STAUTS_OK = 0;
/** Reply status - failed */
public static final int STAUTS_FAILED = 1;
/** Default error code */
public static final String DEFAULT_ERROR_CODE = "UNKNOWN-000";
/** Response status */
private int status = STAUTS_OK;
/** Reply error code, returns */ if there is an error
private String errorCode = DEFAULT_ERROR_CODE;
/** Reply error message, return */ if there is an error
private String errorMsg;
/** Reply error code, returns */ if there is an error
private String errorStack;
/** Reply result, store service reply content */
private T result;
public int getStatus(a) {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getErrorCode(a) {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg(a) {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getErrorStack(a) {
return errorStack;
}
public void setErrorStack(String errorStack) {
this.errorStack = errorStack;
}
public T getResult(a) {
return result;
}
public void setResult(T result) {
this.result = result; }}Copy the code
4. TestGlobalExceptionController. Java classes.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/ * * *@ClassName TestGlobalExceptionController
* @Description TODO
* @AuthorSonorous leaf *@Date 2019/9/29 1:15
* @Version1.0 * * * / javashizhan.com
@RestController
public class TestGlobalExceptionController {
@GetMapping("testGlobalException")
public void exec(a) throws Exception {
throw new Exception("Error occurred"); }}Copy the code
end.
How to understand dependency inversion (IOC) and dependency injection (DI)
Site: javashizhan.com/
Wechat Official Account: