feat: 项目结构重构
This commit is contained in:
43
dolphin-commons/dolphin-common-web/pom.xml
Normal file
43
dolphin-commons/dolphin-common-web/pom.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>day.gitlab</groupId>
|
||||
<artifactId>dolphin-commons</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dolphin-common-web</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<!-- Project -->
|
||||
<dependency>
|
||||
<groupId>day.gitlab</groupId>
|
||||
<artifactId>dolphin-common-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -0,0 +1,43 @@
|
||||
package day.gitlab.dolphin.common.web.entity;
|
||||
|
||||
import day.gitlab.dolphin.common.web.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ApiResponse<T> {
|
||||
|
||||
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;
|
||||
|
||||
public static <T> ApiResponse<T> success() {
|
||||
return ApiResponse.<T>builder().code(SUCCESS).message(SUCCESS_MESSAGE).build();
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> success(T data) {
|
||||
return new ApiResponse<>(SUCCESS, SUCCESS_MESSAGE, data);
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> failure(String code, String message) {
|
||||
return ApiResponse.<T>builder().code(code).message(message).build();
|
||||
}
|
||||
|
||||
public static <T> ApiResponse<T> failure(Exception e) {
|
||||
if (e instanceof BusinessException) {
|
||||
return ApiResponse.<T>builder().code(((BusinessException) e).getCode()).message(e.getMessage()).build();
|
||||
}
|
||||
return ApiResponse.<T>builder().code(FAILURE).message(e.getMessage()).build();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package day.gitlab.dolphin.common.web.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PageRequest<T> {
|
||||
|
||||
private Long pageIndex;
|
||||
private Long pageSize;
|
||||
|
||||
private T query;
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package day.gitlab.dolphin.common.web.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PageResponse<T> {
|
||||
|
||||
private Long pageIndex;
|
||||
private Long pageSize;
|
||||
private Long total;
|
||||
private List<T> records;
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package day.gitlab.dolphin.common.web.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private final String code;
|
||||
|
||||
public BusinessException(String code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package day.gitlab.dolphin.common.web.exception;
|
||||
|
||||
import day.gitlab.dolphin.common.web.entity.ApiResponse;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = BusinessException.class)
|
||||
public <T> ApiResponse<T> handleBusinessException(BusinessException e) {
|
||||
return ApiResponse.failure(e);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public <T> ApiResponse<T> handleException(Exception e) {
|
||||
return ApiResponse.failure(e);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package day.gitlab.dolphin.common.web.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,41 @@
|
||||
package day.gitlab.dolphin.common.web.i18n;
|
||||
|
||||
import day.gitlab.dolphin.common.web.entity.ApiResponse;
|
||||
import day.gitlab.dolphin.common.web.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 BusinessException newBusinessException(String code, Object... args) {
|
||||
return new BusinessException(code, getMessage(code, args));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user