2020-12-22
阿新 • • 發佈:2020-12-22
技術標籤:其它
public class ResponseUtils { /** * 200 OK - [GET]:伺服器成功返回使用者請求的資料 * @param body * @return */ public static ResponseEntity ok() { return new ResponseEntity<>(Collections.EMPTY_MAP, HttpStatus.OK); } public static <T>ResponseEntity ok(T body) { return new ResponseEntity<>(new Content<>(body), HttpStatus.OK); } public static <T>ResponseEntity ok(List<T> list, Long total) { return new ResponseEntity<>(new Page<>(list, total), HttpStatus.OK); } /** * 201 CREATED - [POST/PUT/PATCH]:使用者新建或修改資料成功 * @return */ public static ResponseEntity created() { return new ResponseEntity<>(Collections.EMPTY_MAP, HttpStatus.CREATED); } public static <T>ResponseEntity created(T body) { return new ResponseEntity<>(new Content<>(body), HttpStatus.CREATED); } /** * 202 Accepted - [*]:表示一個請求已經進入後臺排隊(非同步任務) * @return */ public static ResponseEntity accepted() { return new ResponseEntity<>(Collections.EMPTY_MAP, HttpStatus.ACCEPTED); } public static <T>ResponseEntity accepted(T body) { return new ResponseEntity<>(new Content<>(body), HttpStatus.ACCEPTED); } /** * 204 NO CONTENT - [DELETE]:使用者刪除資料成功 * @return */ public static ResponseEntity noContent() { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } }
common-pojo Result 通用物件
ph-smbc-app-api
common-exception
DataNotFoundException
ExistedDataException
InvalidDataException
@JsonSerialize(using = BaseExceptionJackson2Serializer.class) public class BaseException extends RuntimeException { public static final String ERROR = "error"; public static final String ID = "id"; public static final String ERROR_DESCRIPTION = "error_description"; public static final String ERROR_DESCRIPTION_CODE = "error_description_code"; private static final String SEPARATOR = "_"; private String id; private String errorDescriptionCode; private Map<String, String> additionalInformation = null; private Object[] placeholders; // Set default status private HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; public BaseException(final String errorDescriptionCode) { this.errorDescriptionCode = errorDescriptionCode; this.id = getErrorCode() + SEPARATOR + UUID.randomUUID().toString(); } public BaseException(final Throwable cause) { super(cause); this.id = getErrorCode() + SEPARATOR + UUID.randomUUID().toString(); } public BaseException(final String msg, final Throwable t) { super(msg, t); this.id = getErrorCode() + SEPARATOR + UUID.randomUUID().toString(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getErrorCode() { return "error"; } public String getErrorDescriptionCode() { return errorDescriptionCode; } public void setErrorDescriptionCode(String errorDescriptionCode) { this.errorDescriptionCode = errorDescriptionCode; } public HttpStatus getHttpStatus() { return httpStatus; } public BaseException setHttpStatus(final HttpStatus httpStatus) { this.httpStatus = httpStatus; return this; } public Map<String, String> getAdditionalInformation() { return this.additionalInformation; } public Object[] getPlaceholders() { return placeholders; } public BaseException setPlaceholders(final Object... placeholders) { this.placeholders = placeholders; return this; } public BaseException addPlaceholder(final Object placeholder) { ArrayUtils.addAll(this.placeholders, placeholder); return this; } public BaseException addAdditionalInformation(final String key, final String value) { if (this.additionalInformation == null) { this.additionalInformation = new TreeMap<>(); } this.additionalInformation.put(key, value); return this; } @Override public String toString() { return getSummary(); } public String getSummary() { final StringBuilder builder = new StringBuilder(); String delim = ""; final String id = this.getId(); if (id != null) { builder.append(delim).append(ID + "=\"").append(id).append("\""); delim = ", "; } final String error = this.getErrorCode(); if (error != null) { builder.append(delim).append(ERROR + "=\"").append(error).append("\""); delim = ", "; } final String errorMessage = this.getMessage(); if (errorMessage != null) { builder.append(delim).append(ERROR_DESCRIPTION + "=\"").append(errorMessage).append("\""); delim = ", "; } final Map<String, String> additionalParams = this.getAdditionalInformation(); if (additionalParams != null) { for (final Map.Entry<String, String> param : additionalParams.entrySet()) { builder.append(delim).append(param.getKey()).append("=\"").append(param.getValue()).append("\""); delim = ", "; } } return builder.toString(); } }
@JsonSerialize(using = BaseExceptionJackson2Serializer.class) public class BusinessException extends BaseException { public static final String ERRORS = "errors"; private Error[] errors; public BusinessException(String errorDescriptionCode) { super(errorDescriptionCode); } public BusinessException(Throwable cause) { super(cause); } public BusinessException(String msg, Throwable t) { super(msg, t); } public void setErrors(Error... errors) { this.errors = errors; } public Error[] getErrors() { return errors; } public BusinessException addError(Error... error) { ArrayUtils.addAll(this.errors, error); return this; } @Override public String getErrorCode() { return "business_error"; } @Override public HttpStatus getHttpStatus() { return HttpStatus.BAD_REQUEST; } }
public class ServerException extends BaseException {
public ServerException(String msg) {
super(msg);
}
public ServerException(Throwable cause) {
super(cause);
}
public ServerException(String msg, Throwable t) {
super(msg, t);
}
@Override
public String getErrorCode() {
return "server_error";
}
}