feat: 统一响应格式与消息国际化
This commit is contained in:
@ -12,14 +12,17 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- Utilities -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool.v7</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
@ -1,8 +1,4 @@
|
||||
package day.gitlab.dolphin.common.core.constants;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String SUCCESS = "success";
|
||||
|
||||
public static final String FAILURE = "failure";
|
||||
}
|
||||
|
||||
@ -2,54 +2,62 @@ package day.gitlab.dolphin.common.core.entity;
|
||||
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static day.gitlab.dolphin.common.core.constants.Constants.FAILURE;
|
||||
import static day.gitlab.dolphin.common.core.constants.Constants.SUCCESS;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Result {
|
||||
public class Result<T> {
|
||||
|
||||
private int code;
|
||||
public static final String SUCCESS = "00000000";
|
||||
public static final String FAILURE = "99999999";
|
||||
|
||||
public static final String SUCCESS_MESSAGE = "success";
|
||||
public static final String FAILURE_MESSAGE = "failure";
|
||||
|
||||
private String code;
|
||||
private String message;
|
||||
private T data;
|
||||
private long timestamp;
|
||||
|
||||
private Object data;
|
||||
// ========== 通用响应方法 ==========
|
||||
|
||||
public static Result success() {
|
||||
return Result.builder().code(200).message(SUCCESS).build();
|
||||
public static <T> Result<T> result(String code, String message, T data) {
|
||||
return new Result<>(code, message, data, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Result success(Object data) {
|
||||
Result result = Result.builder().code(200).message(SUCCESS).build();
|
||||
if (data instanceof Supplier) {
|
||||
result.setData(((Supplier<?>) data).get());
|
||||
} else {
|
||||
result.setData(data);
|
||||
}
|
||||
return result;
|
||||
// ========== 成功响应方法 ==========
|
||||
|
||||
public static <T> Result<T> success() {
|
||||
return result(SUCCESS, SUCCESS_MESSAGE, null);
|
||||
}
|
||||
|
||||
public static Result failure() {
|
||||
return Result.builder().code(500).message(FAILURE).build();
|
||||
public static <T> Result<T> success(T data) {
|
||||
return result(SUCCESS, SUCCESS_MESSAGE, data);
|
||||
}
|
||||
|
||||
public static Result failure(String message) {
|
||||
return Result.builder().code(500).message(message).build();
|
||||
public static <T> Result<T> success(String message, T data) {
|
||||
return result(SUCCESS, message, data);
|
||||
}
|
||||
|
||||
public static Result failure(int code, String message) {
|
||||
return Result.builder().code(code).message(message).build();
|
||||
// ========== 失败响应方法 ==========
|
||||
|
||||
public static <T> Result<T> failure() {
|
||||
return result(FAILURE, FAILURE_MESSAGE, null);
|
||||
}
|
||||
|
||||
public static Result failure(BusinessException businessException) {
|
||||
return Result.builder().code(businessException.getCode()).message(businessException.getMessage()).build();
|
||||
public static <T> Result<T> failure(String message) {
|
||||
return result(FAILURE, message, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failure(String code, String message) {
|
||||
return result(code, message, null);
|
||||
}
|
||||
|
||||
// ========== 异常响应方法 ==========
|
||||
|
||||
public static <T> Result<T> failure(BusinessException e) {
|
||||
return result(e.getCode(), e.getMessage(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,14 +5,9 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private final int code;
|
||||
private final String code;
|
||||
|
||||
public BusinessException(String message) {
|
||||
super(message);
|
||||
this.code = 500;
|
||||
}
|
||||
|
||||
public BusinessException(int code, String message) {
|
||||
public BusinessException(String code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@ -8,12 +8,12 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = BusinessException.class)
|
||||
public Result handleBusinessException(BusinessException e) {
|
||||
public <T> Result<T> handleBusinessException(BusinessException e) {
|
||||
return Result.failure(e);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public Result handleException(Exception e) {
|
||||
public <T> Result<T> handleException(Exception e) {
|
||||
return Result.failure(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package day.gitlab.dolphin.common.core.i18n;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Configuration
|
||||
public class MessagesConfiguration {
|
||||
|
||||
@Bean
|
||||
public MessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("i18n.messages");
|
||||
messageSource.setDefaultEncoding("UTF-8");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
|
||||
localeResolver.setDefaultLocale(Locale.ENGLISH);
|
||||
return localeResolver;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package day.gitlab.dolphin.common.core.i18n;
|
||||
|
||||
import day.gitlab.dolphin.common.core.entity.Result;
|
||||
import day.gitlab.dolphin.common.core.exception.BusinessException;
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Getter
|
||||
@Component
|
||||
public class MessagesHelper {
|
||||
|
||||
private final MessageSource messageSource;
|
||||
|
||||
public MessagesHelper(MessageSource messageSource) {
|
||||
this.messageSource = messageSource;
|
||||
}
|
||||
|
||||
// ========== 国际化消息 ==========
|
||||
|
||||
public Locale getCurrentLocale() {
|
||||
return LocaleContextHolder.getLocale();
|
||||
}
|
||||
|
||||
public String getMessage(String code, Object... args) {
|
||||
return messageSource.getMessage(code, args, getCurrentLocale());
|
||||
}
|
||||
|
||||
public String getMessage(String code, String defaultMessage, Object... args) {
|
||||
return messageSource.getMessage(code, args, defaultMessage, getCurrentLocale());
|
||||
}
|
||||
|
||||
// ========== 成功响应方法 ==========
|
||||
|
||||
public <T> Result<T> success() {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
public <T> Result<T> success(T data) {
|
||||
return Result.success(data);
|
||||
}
|
||||
|
||||
// ========== 业务异常快捷方法 ==========
|
||||
|
||||
public BusinessException newBusinessException(String code, Object... args) {
|
||||
return new BusinessException(code, getMessage(code, args));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user