From f7c036795d6c422b87482ebab70c524bf4b0c053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=AF=85=E9=AA=8F?= Date: Fri, 26 Aug 2022 00:35:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 16 ++- zyjblogs-common-spring-boot-starter/pom.xml | 3 +- .../{ResponseStatus.java => HttpCode.java} | 28 ++-- .../entity/response/ResponseResult.java | 12 +- .../pom.xml | 7 +- zyjblogs-gateway/pom.xml | 7 +- .../zyjblogs/gateway/filter/AuthFilter.java | 9 +- zyjblogs-oauth/pom.xml | 7 +- .../oauth/config/security/JwtTokenConfig.java | 3 +- zyjblogs-rbac/pom.xml | 7 +- zyjblogs-web-spring-boot-starter/pom.xml | 17 ++- .../Knife4jAutoConfigurationConfig.java | 110 --------------- .../web/hander/GlobalExceptionHandler.java | 132 ++++++++++++++++++ .../src/main/resources/META-INF/MANIFEST.MF | 5 + .../pom.properties | 5 + .../pom.xml | 59 ++++++++ .../main/resources/META-INF/spring.factories | 3 + 17 files changed, 275 insertions(+), 155 deletions(-) rename zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/{ResponseStatus.java => HttpCode.java} (87%) delete mode 100644 zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/config/Knife4jAutoConfigurationConfig.java create mode 100644 zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java create mode 100644 zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/MANIFEST.MF create mode 100644 zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.properties create mode 100644 zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.xml create mode 100644 zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/pom.xml b/pom.xml index b05480f..f7debf9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 2.6.0 + 2.3.12.RELEASE cn.zyjblogs @@ -25,6 +25,7 @@ 1.0-SNAPSHOT 11 11 + 2.3.12.RELEASE Hoxton.SR12 1.18.24 @@ -204,11 +205,6 @@ commons-crypto ${commons-crypto.version} - - ma.glasnost.orika - orika-core - ${orika-core.version} - @@ -260,6 +256,14 @@ org.springframework.boot spring-boot-maven-plugin + ${compler.maven.plugin.version} + + + + repackage + + + diff --git a/zyjblogs-common-spring-boot-starter/pom.xml b/zyjblogs-common-spring-boot-starter/pom.xml index 01b8ea0..86da8b4 100644 --- a/zyjblogs-common-spring-boot-starter/pom.xml +++ b/zyjblogs-common-spring-boot-starter/pom.xml @@ -10,7 +10,7 @@ 4.0.0 cn.zyjblogs.starter zyjblogs-common-spring-boot-starter - ${zyjblogs.version} + 1.0-SNAPSHOT 11 11 @@ -76,6 +76,7 @@ org.springframework.boot spring-boot-maven-plugin + ${compler.maven.plugin.version} true diff --git a/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseStatus.java b/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java similarity index 87% rename from zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseStatus.java rename to zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java index b51b425..08d37d6 100644 --- a/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseStatus.java +++ b/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java @@ -1,6 +1,6 @@ package cn.zyjblogs.starter.common.entity.response; -public enum ResponseStatus { +public enum HttpCode { CONTINUE(100, "Continue"), SWITCHING_PROTOCOLS(101, "Switching Protocols"), PROCESSING(102, "Processing"), @@ -87,7 +87,7 @@ public enum ResponseStatus { private final int value; private final String reasonPhrase; - private ResponseStatus(int value, String reasonPhrase) { + private HttpCode(int value, String reasonPhrase) { this.value = value; this.reasonPhrase = reasonPhrase; } @@ -101,27 +101,27 @@ public enum ResponseStatus { } public Series series() { - return ResponseStatus.Series.valueOf(this); + return HttpCode.Series.valueOf(this); } public boolean is1xxInformational() { - return this.series() == ResponseStatus.Series.INFORMATIONAL; + return this.series() == HttpCode.Series.INFORMATIONAL; } public boolean is2xxSuccessful() { - return this.series() == ResponseStatus.Series.SUCCESSFUL; + return this.series() == HttpCode.Series.SUCCESSFUL; } public boolean is3xxRedirection() { - return this.series() == ResponseStatus.Series.REDIRECTION; + return this.series() == HttpCode.Series.REDIRECTION; } public boolean is4xxClientError() { - return this.series() == ResponseStatus.Series.CLIENT_ERROR; + return this.series() == HttpCode.Series.CLIENT_ERROR; } public boolean is5xxServerError() { - return this.series() == ResponseStatus.Series.SERVER_ERROR; + return this.series() == HttpCode.Series.SERVER_ERROR; } public boolean isError() { @@ -132,8 +132,8 @@ public enum ResponseStatus { return this.value + " " + this.name(); } - public static ResponseStatus valueOf(int statusCode) { - ResponseStatus status = resolve(statusCode); + public static HttpCode valueOf(int statusCode) { + HttpCode status = resolve(statusCode); if (status == null) { throw new IllegalArgumentException("No matching constant for [" + statusCode + "]"); } else { @@ -142,12 +142,12 @@ public enum ResponseStatus { } - public static ResponseStatus resolve(int statusCode) { - ResponseStatus[] var1 = values(); + public static HttpCode resolve(int statusCode) { + HttpCode[] var1 = values(); int var2 = var1.length; for(int var3 = 0; var3 < var2; ++var3) { - ResponseStatus status = var1[var3]; + HttpCode status = var1[var3]; if (status.value == statusCode) { return status; } @@ -173,7 +173,7 @@ public enum ResponseStatus { return this.value; } - public static Series valueOf(ResponseStatus status) { + public static Series valueOf(HttpCode status) { return valueOf(status.value); } diff --git a/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java b/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java index f1c6e71..d510ad1 100644 --- a/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java +++ b/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java @@ -21,21 +21,21 @@ public class ResponseResult { } public static ResponseObject success(long total, T data) { - return ResponseObject.builder().code(ResponseStatus.OK.value()).msg(ResponseStatus.OK.getReasonPhrase()).count(total).data(data).timestamp(System.currentTimeMillis()).build(); + return ResponseObject.builder().code(HttpCode.OK.value()).msg(HttpCode.OK.getReasonPhrase()).count(total).data(data).timestamp(System.currentTimeMillis()).build(); } - public static ResponseObject error(ResponseStatus responseCode, String errorMsg) { + public static ResponseObject error(HttpCode responseCode, String errorMsg) { return error(responseCode, responseCode.getReasonPhrase(), errorMsg, 0L, null); } - public static ResponseObject error(ResponseStatus responseCode, String errorCode, String errorMsg) { + public static ResponseObject error(HttpCode responseCode, String errorCode, String errorMsg) { return error(responseCode, errorCode, errorMsg, 0L, null); } - public static ResponseObject error(ResponseStatus responseCode, String errorCode, String errorMsg, T data) { + public static ResponseObject error(HttpCode responseCode, String errorCode, String errorMsg, T data) { return error(responseCode, errorCode, errorMsg, 1L, data); } - public static ResponseObject error(ResponseStatus responseCode, String errorCode, String errorMsg, long total, T data) { - if (responseCode.value() == ResponseStatus.OK.value()) { + public static ResponseObject error(HttpCode responseCode, String errorCode, String errorMsg, long total, T data) { + if (responseCode.value() == HttpCode.OK.value()) { throw new IllegalArgumentException("ResponseResult.error方法中的responseCode参数不能是ResponseCode.REQUEST_SUCCESS"); } else { return ResponseObject.builder().code(responseCode.value()).errorCode(errorCode).msg(errorMsg == null ? responseCode.getReasonPhrase() : errorMsg).count(total).data(data).timestamp(System.currentTimeMillis()).build(); diff --git a/zyjblogs-common-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-common-spring-boot-starter/pom.xml b/zyjblogs-common-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-common-spring-boot-starter/pom.xml index bfc9f21..2137c1f 100644 --- a/zyjblogs-common-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-common-spring-boot-starter/pom.xml +++ b/zyjblogs-common-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-common-spring-boot-starter/pom.xml @@ -10,12 +10,13 @@ 4.0.0 cn.zyjblogs.starter zyjblogs-common-spring-boot-starter - ${zyjblogs.version} + 1.0-SNAPSHOT 11 11 + org.springframework.boot spring-boot-starter-validation @@ -61,6 +62,10 @@ io.jsonwebtoken jjwt + + org.bouncycastle + bcprov-jdk15on + javax.xml.bind jaxb-api diff --git a/zyjblogs-gateway/pom.xml b/zyjblogs-gateway/pom.xml index ec25d20..2960e2c 100644 --- a/zyjblogs-gateway/pom.xml +++ b/zyjblogs-gateway/pom.xml @@ -11,7 +11,7 @@ cn.zyjblogs.pubilc zyjblogs-gateway - ${zyjblogs.version} + 1.0-SNAPSHOT 11 @@ -40,6 +40,11 @@ org.apache.commons commons-pool2 + + zyjblogs-web-spring-boot-starter + cn.zyjblogs.starter + ${zyjblogs.version} + com.alibaba.cloud diff --git a/zyjblogs-gateway/src/main/java/cn/zyjblogs/gateway/filter/AuthFilter.java b/zyjblogs-gateway/src/main/java/cn/zyjblogs/gateway/filter/AuthFilter.java index ca0545d..ba149c0 100644 --- a/zyjblogs-gateway/src/main/java/cn/zyjblogs/gateway/filter/AuthFilter.java +++ b/zyjblogs-gateway/src/main/java/cn/zyjblogs/gateway/filter/AuthFilter.java @@ -3,7 +3,7 @@ package cn.zyjblogs.gateway.filter; import cn.zyjblogs.starter.common.entity.constant.HttpHeaderConstant; import cn.zyjblogs.starter.common.entity.response.ResponseObject; import cn.zyjblogs.starter.common.entity.response.ResponseResult; -import cn.zyjblogs.starter.common.entity.response.ResponseStatus; +import cn.zyjblogs.starter.common.entity.response.HttpCode; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; @@ -15,7 +15,6 @@ import org.springframework.cloud.gateway.route.Route; import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; @@ -66,7 +65,7 @@ public class AuthFilter implements GlobalFilter { } if (isExpired(token)) { log.info("token过期"); - return getErrorMono(response, ResponseStatus.UNAUTHORIZED, "invalid_token"); + return getErrorMono(response, HttpCode.UNAUTHORIZED, "invalid_token"); } if ("/user/login".equals(path)) { return chain.filter(exchange); @@ -131,9 +130,9 @@ public class AuthFilter implements GlobalFilter { * @return reactor.core.publisher.Mono * @date 2021/11/1 17:10 */ - private Mono getErrorMono(ServerHttpResponse response, ResponseStatus responseCode, String msg) { + private Mono getErrorMono(ServerHttpResponse response, HttpCode responseCode, String msg) { response.getHeaders().setContentType(MediaType.APPLICATION_JSON); - response.setStatusCode(HttpStatus.OK); + response.setStatusCode(org.springframework.http.HttpStatus.OK); ResponseObject responseObject = ResponseResult.error(responseCode, msg); ObjectMapper mapper = new ObjectMapper(); byte[] bytes = new byte[0]; diff --git a/zyjblogs-oauth/pom.xml b/zyjblogs-oauth/pom.xml index 645acac..752939f 100644 --- a/zyjblogs-oauth/pom.xml +++ b/zyjblogs-oauth/pom.xml @@ -11,7 +11,7 @@ cn.zyjblogs.pubilc zyjblogs-oauth - ${zyjblogs.version} + 1.0-SNAPSHOT 11 @@ -22,6 +22,11 @@ org.springframework.boot spring-boot-starter-web + + zyjblogs-web-spring-boot-starter + cn.zyjblogs.starter + ${zyjblogs.version} + org.springframework.boot spring-boot-starter-security diff --git a/zyjblogs-oauth/src/main/java/cn/zyjblogs/oauth/config/security/JwtTokenConfig.java b/zyjblogs-oauth/src/main/java/cn/zyjblogs/oauth/config/security/JwtTokenConfig.java index f7e02f0..4f5e443 100644 --- a/zyjblogs-oauth/src/main/java/cn/zyjblogs/oauth/config/security/JwtTokenConfig.java +++ b/zyjblogs-oauth/src/main/java/cn/zyjblogs/oauth/config/security/JwtTokenConfig.java @@ -1,7 +1,6 @@ package cn.zyjblogs.oauth.config.security; - -import io.micrometer.core.instrument.util.IOUtils; import lombok.RequiredArgsConstructor; +import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/zyjblogs-rbac/pom.xml b/zyjblogs-rbac/pom.xml index deabfc0..9b19932 100644 --- a/zyjblogs-rbac/pom.xml +++ b/zyjblogs-rbac/pom.xml @@ -10,7 +10,7 @@ 4.0.0 cn.zyjblogs.pubilc zyjblogs-rbac - ${zyjblogs.version} + 1.0-SNAPSHOT 11 @@ -21,6 +21,11 @@ org.springframework.boot spring-boot-starter-web + + zyjblogs-web-spring-boot-starter + cn.zyjblogs.starter + ${zyjblogs.version} + org.springframework.boot spring-boot-starter-security diff --git a/zyjblogs-web-spring-boot-starter/pom.xml b/zyjblogs-web-spring-boot-starter/pom.xml index e7eadbf..1bd4807 100644 --- a/zyjblogs-web-spring-boot-starter/pom.xml +++ b/zyjblogs-web-spring-boot-starter/pom.xml @@ -10,6 +10,8 @@ 4.0.0 zyjblogs-web-spring-boot-starter + cn.zyjblogs.starter + 1.0-SNAPSHOT 11 @@ -21,14 +23,14 @@ org.springframework.boot spring-boot-starter-web + + + + - com.github.xiaoymin - knife4j-spring-boot-starter - - - - io.swagger - swagger-annotations + cn.zyjblogs.starter + zyjblogs-common-spring-boot-starter + ${zyjblogs.version} org.springframework.boot @@ -48,6 +50,7 @@ org.springframework.boot spring-boot-maven-plugin + ${compler.maven.plugin.version} true diff --git a/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/config/Knife4jAutoConfigurationConfig.java b/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/config/Knife4jAutoConfigurationConfig.java deleted file mode 100644 index 1afad45..0000000 --- a/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/config/Knife4jAutoConfigurationConfig.java +++ /dev/null @@ -1,110 +0,0 @@ -// -// Source code recreated from a .class file by IntelliJ IDEA -// (powered by FernFlower decompiler) -// - -package cn.zyjblogs.starter.web.config; - -import com.google.common.collect.Lists; -import java.lang.reflect.Field; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZonedDateTime; -import java.util.Date; -import java.util.List; -import java.util.stream.Collectors; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.context.annotation.Bean; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.stereotype.Component; -import org.springframework.util.ReflectionUtils; -import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; -import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.oas.annotations.EnableOpenApi; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.ApiKey; -import springfox.documentation.service.AuthorizationScope; -import springfox.documentation.service.Contact; -import springfox.documentation.service.SecurityReference; -import springfox.documentation.service.SecurityScheme; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spi.service.contexts.SecurityContext; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; -import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; - -import java.lang.reflect.Field; -import java.util.List; -import java.util.stream.Collectors; - -@EnableOpenApi -@Configuration -@Import({BeanValidatorPluginsConfiguration.class}) -public class Knife4jAutoConfigurationConfig { - @Value("${spring.application.name}") - private String applicationName; - - public Knife4jAutoConfigurationConfig() { - } - - @Bean - public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { - return new BeanPostProcessor() { - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { - if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { - this.customizeSpringfoxHandlerMappings(this.getHandlerMappings(bean)); - } - - return bean; - } - - private void customizeSpringfoxHandlerMappings(List mappings) { - List copy = (List)mappings.stream().filter((mapping) -> mapping.getPatternParser() == null).collect(Collectors.toList()); - mappings.clear(); - mappings.addAll(copy); - } - - private List getHandlerMappings(Object bean) { - try { - Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); - field.setAccessible(true); - return (List)field.get(bean); - } catch (IllegalAccessException | IllegalArgumentException var3) { - throw new IllegalStateException(var3); - } - } - }; - } - - @Bean({"defaultApi"}) - public Docket defaultApi() { - return (new Docket(DocumentationType.SWAGGER_2)).directModelSubstitute(LocalDateTime.class, Date.class).directModelSubstitute(LocalDate.class, String.class).directModelSubstitute(LocalTime.class, String.class).directModelSubstitute(ZonedDateTime.class, String.class).apiInfo(this.apiInfo()).select().apis(RequestHandlerSelectors.basePackage("cn.zyjblogs")).paths(PathSelectors.any()).build().securityContexts(Lists.newArrayList(new SecurityContext[]{this.securityContext(), this.securityContext()})).securitySchemes(Lists.newArrayList(new SecurityScheme[]{this.apiKey()})); - } - - private ApiInfo apiInfo() { - return (new ApiInfoBuilder()).title(this.applicationName.toUpperCase() + " RESTFUL APIS").contact(new Contact("zyjblogs", "", "")).version("1.0.0").build(); - } - - private ApiKey apiKey() { - return new ApiKey("BearerToken", "Authorization", "header"); - } - - private SecurityContext securityContext() { - return SecurityContext.builder().securityReferences(this.defaultAuth()).forPaths(PathSelectors.regex("/.*")).build(); - } - - private List defaultAuth() { - AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); - AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope}; - return Lists.newArrayList(new SecurityReference[]{new SecurityReference("BearerToken", authorizationScopes)}); - } -} diff --git a/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java b/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java new file mode 100644 index 0000000..35ffe0c --- /dev/null +++ b/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java @@ -0,0 +1,132 @@ +// +// Source code recreated from a .class file by IntelliJ IDEA +// (powered by FernFlower decompiler) +// + +package cn.zyjblogs.starter.web.hander; + +import cn.zyjblogs.starter.common.entity.response.HttpCode; +import cn.zyjblogs.starter.common.entity.response.ResponseObject; +import cn.zyjblogs.starter.common.entity.response.ResponseResult; +import cn.zyjblogs.starter.common.exception.AbstractFrameworkException; +import java.util.List; +import java.util.Set; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import org.apache.commons.collections4.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.validation.BindException; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.springframework.web.servlet.NoHandlerFoundException; + +@RestControllerAdvice +public class GlobalExceptionHandler { + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + private static final String TYPE_MISMATCH = "typeMismatch"; + + @ExceptionHandler({NoHandlerFoundException.class}) + @ResponseStatus(org.springframework.http.HttpStatus.NOT_FOUND) + public ResponseObject handleNoHandlerFoundException(NoHandlerFoundException exception) { + log.error("NoHandlerFound", exception); + String i18nCode = "web_starter_not_found"; + return ResponseResult.error(HttpCode.NOT_FOUND, i18nCode, i18nCode); + } + + @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) + public ResponseObject handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) { + log.error("HttpRequestMethodNotSupported", exception); + String i18nCode = "web_starter_method_not_allowed"; + return ResponseResult.error(HttpCode.METHOD_NOT_ALLOWED, i18nCode, i18nCode); + } + + @ExceptionHandler({HttpMediaTypeNotSupportedException.class}) + public ResponseObject handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException exception) { + log.error("HttpMediaTypeNotSupported", exception); + String i18nCode = "web_starter_unsupported_media_type"; + return ResponseResult.error(HttpCode.UNSUPPORTED_MEDIA_TYPE, i18nCode, i18nCode); + } + + @ExceptionHandler({Exception.class}) + public ResponseObject handleException(Exception exception) { + log.error("", exception); + String i18nCode = "web_starter_internal_server_error"; + return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, i18nCode, i18nCode); + } + + @ExceptionHandler({MethodArgumentTypeMismatchException.class}) + public ResponseObject handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception) { + String parameterName = exception.getParameter().getParameterName(); + String i18nCode = "web_starter_property_format_error"; + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } + + @ExceptionHandler({ConstraintViolationException.class}) + public ResponseObject handleConstraintViolationException(ConstraintViolationException exception) { + Set> constraintViolations = exception.getConstraintViolations(); + String i18nCode; + if (CollectionUtils.isEmpty(constraintViolations)) { + log.warn("全局异常捕获到了ConstraintViolationException,但是没有发现错误", exception); + i18nCode = "web_starter_bad_request"; + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } else { + i18nCode = ((ConstraintViolation)constraintViolations.stream().findFirst().get()).getMessage(); + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } + } + + @ExceptionHandler({MethodArgumentNotValidException.class}) + public ResponseObject handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) { + BindingResult bindingResult = exception.getBindingResult(); + return this.getByBindingResult(bindingResult); + } + + @ExceptionHandler({AbstractFrameworkException.class}) + public ResponseObject handleFrameworkException(AbstractFrameworkException exception) { + log.error("系统异常:", exception); + String i18nCode = "web_starter_internal_server_error"; + return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, i18nCode, i18nCode); + } + + @ExceptionHandler({BindException.class}) + public ResponseObject handleBindException(BindException exception) { + BindingResult bindingResult = exception.getBindingResult(); + return this.getByBindingResult(bindingResult); + } + + @ExceptionHandler({HttpMessageNotReadableException.class}) + public ResponseObject handleHttpMessageNotReadableException(HttpMessageNotReadableException exception) { + log.error("参数格式不正确", exception); + String i18nCode = "web_starter_param_format_error"; + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } + + private ResponseObject getByBindingResult(BindingResult bindingResult) { + List fieldErrors = bindingResult.getFieldErrors(); + if (fieldErrors.isEmpty()) { + log.error("捕获到了参数校验错误,但是没有找到错误提示的i18nCode,{}", bindingResult); + String i18nCode = "web_starter_parameter_error"; + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } else { + FieldError fieldError = (FieldError)fieldErrors.get(0); + String i18nCode; + if ("typeMismatch".equals(fieldError.getCode())) { + i18nCode = "web_starter_property_format_error"; + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } else { + i18nCode = fieldError.getDefaultMessage(); + return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + } + } + } +} diff --git a/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/MANIFEST.MF b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..966deb6 --- /dev/null +++ b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Archiver-Version: Plexus Archiver +Created-By: Apache Maven 3.6.3 +Built-By: mitom +Build-Jdk: 11 diff --git a/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.properties b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.properties new file mode 100644 index 0000000..7e46149 --- /dev/null +++ b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Mon Jul 25 15:29:06 CST 2022 +groupId=cn.zyjblogs.starter +artifactId=zyjblogs-web-spring-boot-starter +version=1.0-SNAPSHOT diff --git a/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.xml b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.xml new file mode 100644 index 0000000..6375283 --- /dev/null +++ b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/maven.cn.zyjblogs.starter.zyjblogs-web-spring-boot-starter/pom.xml @@ -0,0 +1,59 @@ + + + + zyjblogs-parent + cn.zyjblogs + 1.0-SNAPSHOT + + 4.0.0 + + zyjblogs-web-spring-boot-starter + cn.zyjblogs.starter + 1.0-SNAPSHOT + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.github.xiaoymin + knife4j-spring-boot-starter + + + cn.zyjblogs.starter + zyjblogs-common-spring-boot-starter + ${zyjblogs.version} + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-configuration-processor + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + \ No newline at end of file diff --git a/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..898d6f8 --- /dev/null +++ b/zyjblogs-web-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Auto Configure +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.zyjblogs.starter.web.config.Knife4jAutoConfigurationConfig