@ApiOperation(value = "New account") @PostMapping("/addAccount") public Result<Object> addAccount(@Valid @RequestBody AddAccountVo vo) { usrAccountService.addAccount(vo); return Result.success(RespCode.SUCCESS); } @NotBlank(message = "mobile cannot be empty") @ApiModelProperty(value = "Mobile phone number") private String mobile; @ApiModelProperty(value = "email") private String email; @ApiModelProperty(value = "Department name-splicing") private String deptIdListName; @ApiModelProperty(value = "Start visible permission data collection") @NotEmpty(message = "enabledList cannot be empty") private List<SsoClientAccountAddVo> enabledList; public List<SsoClientAccountAddVo> getEnable
Custom global exception handling class:
@Order(-1000) @Configuration public class ExceptionHandler implements HandlerExceptionResolver { private static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandler.class); @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()); HashMap<String, Object> result = new HashMap<>(); result.put("success",false); if (ex instanceof ServiceException) { result.put("code", ((ServiceException) ex).getCode()); result.put("msg", StringUtils.hasLength(((ServiceException) ex).getMsg()) ? ((ServiceException) ex).getMsg(): ex.getMessage() ); if(StringUtils.hasLength(((ServiceException) ex).getData())){ result.put("data", JSONObject.parse(((ServiceException) ex).getData())); } result.put("timestamp",System.currentTimeMillis()); result.put("traceId",((ServiceException) ex).getTraceId()); LOGGER.error("Business exception:" + (StringUtils.hasLength(((ServiceException) ex).getMsg()) ? ((ServiceException) ex).getMsg(): ex.getMessage())); }else if (ex instanceof MethodArgumentNotValidException) { result.put("code", 9999); result.put("msg", ((MethodArgumentNotValidException) ex).getBindingResult().getFieldError().getDefaultMessage()); result.put("timestamp",System.currentTimeMillis()); result.put("traceId", UUID.randomUUID().toString().replace("-", "")); LOGGER.info("Parameter exception RebuildServiceException: {}", result); } else { ex.printStackTrace(); result.put("code", RespCode.END.getCode()); result.put("msg", RespCode.END.getMsg()); result.put("timestamp",System.currentTimeMillis()); result.put("traceId", UUID.randomUUID().toString().replace("-", "")); LOGGER.error(RespCode.END.getMsg()); } response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache, must-revalidate"); try { modelAndView.addAllObjects(result); } catch (Exception e) { LOGGER.error("#Communication exception with client:" + e.getMessage(), e); } return modelAndView; }
Custom business exception class
public class ServiceException extends RuntimeException { private static final long serialVersionUID = 3653415555548581494L; private String code; private String msg; private String data; private String traceId= UUID.randomUUID().toString().replace("-", ""); private RespCode respCode; public RespCode getRespCode() { return respCode; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getData() { return data; } public void setData(String data) { this.data = data; } public void setRespCode(RespCode respCode) { this.respCode = respCode; } @SuppressWarnings("unused") private ServiceException() { } public ServiceException(String msg, Throwable e) { super(msg, e); this.msg = msg; } public ServiceException(String msg) { super(msg); this.msg = msg; } public ServiceException(RespCode respCode) { super(respCode.getCode() + respCode.getMsg()); this.code = respCode.getCode(); this.msg = respCode.getMsg(); this.respCode=respCode; } public ServiceException(String data, RespCode respCode) { super(respCode.getCode() + respCode.getMsg()); this.code = respCode.getCode(); this.msg = respCode.getMsg(); this.data=data; this.respCode=respCode; } public ServiceException(RespCode respCode, Object... moreMsg) { super(respCode.getCode() + String.format(respCode.getMsg(), moreMsg)); this.code = respCode.getCode(); try { msg = String.format(respCode.getMsg(), moreMsg); } catch (Exception e) { msg = respCode.getMsg(); } } public ServiceException(String data, RespCode respCode, Object... moreMsg) { super(respCode.getCode() + respCode.getMsg()); this.code = respCode.getCode(); try { msg = String.format(respCode.getMsg(), moreMsg); } catch (Exception e) { msg = respCode.getMsg(); } this.data=data; this.respCode=respCode; } public String getTraceId() { return traceId; } public void setTraceId(String traceId) { this.traceId = traceId; }
/** * Return results uniformly * @author: jly * @date: 2023/4/6 */ public class Result<T> { private String code; private String msg; private String traceId; private Long timestamp; private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getTraceId() { return traceId; } public void setTraceId(String traceId) { this.traceId = traceId; } public Long getTimestamp() { return timestamp; } public void setTimestamp(Long timestamp) { this.timestamp = timestamp; } public T getData() { return data; } public void setData(T data) { this.data = data; } public static <T> Result<T> success(T body) { Result<T> res = new Result<>(); res.setCode(RespCode.SUCCESS.getCode()); res.setMsg(RespCode.SUCCESS.getMsg()); res.setData(body); res.setTraceId(UUID.randomUUID().toString().replace("-", "")); res.setTimestamp(System.currentTimeMillis()); return res; } public static <T> Result<T> fail(T body) { Result<T> res = new Result<>(); res.setCode(RespCode.SUCCESS.getCode()); res.setMsg(RespCode.SUCCESS.getMsg()); res.setData(body); res.setTraceId(UUID.randomUUID().toString().replace("-", "")); res.setTimestamp(System.currentTimeMillis()); return res; } }
/** * Return result code * * @author: jly * @date: 2023/4/6 */ public enum RespCode { /** \t * Successful operation */ SUCCESS("0000", "Operation successful"), PARAM_ILLEGAL("0001", "Parameter [%s] is illegal, %s"), END("0002", "The system is busy, please try again later"), PARAMETER_FAIL("0003", "Parameter missing, %s"), ; private String code; private String msg; RespCode(String code, String msg) { this.code = code; this.msg = msg; } public static RespCode getRespByCode(String code) { if (code == null) { return null; } for (RespCode resp : values()) { if (resp.getCode().equals(code)) { return resp; } } throw new IllegalArgumentException("Invalid code value!code:" + code); } public String getCode() { return code; } public String getMsg() { return msg; } public boolean isSuccess(String code) { return Objects.equals(code, this.code); } }
Possibly used dependencies <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.2.0.Final</version> </dependency>
<dependency> <groupId>fa.hiveware</groupId> <artifactId>hiveware-cmn-contract</artifactId> <version>1.0</version> </dependency>
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137920 people are learning the system