First, unified result return :
import io.swagger.annotations.ApiModelProperty; import lombok.Data; import
java.util.HashMap; import java.util.Map; /** * Unified return data type */ @Data public class
R { @ApiModelProperty(value = " Success or not ") private Boolean success;
@ApiModelProperty(value = " Return code ") private Integer code; @ApiModelProperty(value
= " Return message ") private String message; @ApiModelProperty(value = " Return data ") private
Map<String, Object> data = new HashMap<String, Object>(); // Privatization construction method private
R(){} public static R ok(){ R r = new R(); r.setSuccess(true);
r.setCode(ResultCode.SUCCESS); r.setMessage(" success "); return r; } public static R
error(){ R r = new R(); r.setSuccess(false); r.setCode(ResultCode.Error);
r.setMessage(" fail "); return r; } public R success(Boolean success){
this.setSuccess(success); return this; } public R message(String message){
this.setMessage(message); return this; } public R code(Integer code){
this.setCode(code); return this; } public R data(String key, Object value){
this.data.put(key, value); return this; } public R data(Map<String, Object>
map){ this.setData(map); return this; } }
Defined return code interface ( You can also enumerate ):
public interface ResultCode { Integer SUCCESS = 200; Integer Error = 500; }
Global exception handling :
import com.gbx.vue.test.common.common.utils.R; import
org.springframework.web.bind.annotation.ControllerAdvice; import
org.springframework.web.bind.annotation.ExceptionHandler; import
org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public
class GlobalExceptionHandler { @ExceptionHandler(Exception.class) // Specify the exception category to handle
@ResponseBody // wrong RestController To return results public R error(Exception e){ String
message = " Global exception handling ...."; // According to Specific types of exceptions Add different parameters and information to Custom result return unified class in if(e instanceof
NullPointerException){ message = " Null pointer exception "; } return R.error().message(message);
} }
 

Technology
©2019-2020 Toolsou All rights reserved,
CSS architecture design NOI2019 travels Detailed explanation ubuntu14.04 How to set static IP2019 Year end summary —— Second year of work Big data tells you , How tired are Chinese women java Simple lottery algorithm , luck draw DemoPYTHON Summary of final review Classical algorithm - recursion ( The case of raw rabbit )2021 year 1 Monthly programmer salary statistics , average 14915 element First knowledge python Skills summary