diff --git a/README.md b/README.md index 57e4bdbe46e0b6bc1cafb94bc67a2bc66de01847..043be9e68612fcb85b87905ae0e3cc5c5b9b4f6d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ## mica(云母) [![Mica Maven](https://img.shields.io/maven-central/v/net.dreamlu/mica-bom.svg?style=flat-square)](https://mvnrepository.com/artifact/net.dreamlu/mica-bom) -`Mica`,Spring Cloud 微服务开发核心包,基于 `Spring boot 2.x`,暂不支持 `webflux`。 +`Mica`,Spring Cloud 微服务开发核心包,基于 `Spring boot 2.x`,支持 `web` 和 `webflux`。 想要了解更多可加入【如梦技术】QQ群:`479710041` @@ -25,6 +25,7 @@ - spi 扩展 ### mica-boot +- 支持 `Spring boot web` 和 `Spring boot webflux`。 - 异步配置。 - 异常处理,未知异常发送 Event 事件,方便监听收集。 - swagger自动化配置,加入jar包即可。 diff --git a/gradle.properties b/gradle.properties index a6c154b4926eff7b389520b89cf65a723fea5b3b..0794465f36f18dbce0e268f9adebd8d5f8565e33 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION=0.0.1-RC3 +VERSION=0.0.1-RC4 GROUPID=net.dreamlu NEXUS_OSS_USER_NAME=*** diff --git a/mica-boot/build.gradle b/mica-boot/build.gradle index d5313b8e8e6845ea0b2285cb622665672a233677..c3070b4b8d6e963a206c54467a91b38f4a7dd195 100644 --- a/mica-boot/build.gradle +++ b/mica-boot/build.gradle @@ -4,6 +4,7 @@ dependencies { api "org.springframework.boot:spring-boot-starter-aop" compileOnly "org.springframework.cloud:spring-cloud-context" implementation "io.springfox:springfox-swagger2:${swaggerVersion}" + implementation "org.springframework.boot:spring-boot-starter-webflux" implementation("org.springframework.boot:spring-boot-starter-web") { exclude group: "org.springframework.boot", module: "spring-boot-starter-tomcat" } diff --git a/mica-boot/src/main/java/net/dreamlu/mica/common/error/BaseExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/common/error/BaseExceptionTranslator.java new file mode 100644 index 0000000000000000000000000000000000000000..65a6b310a0daaf64e8e8c01edd5ca22cb0ed9752 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/common/error/BaseExceptionTranslator.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.common.error; + +import net.dreamlu.mica.core.result.R; +import net.dreamlu.mica.core.result.SystemCode; +import org.hibernate.validator.internal.engine.path.PathImpl; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; + +import javax.validation.ConstraintViolation; +import java.util.Set; + +/** + * 抽取共性 通用部分代码 + * + * @author L.cm + */ +public abstract class BaseExceptionTranslator { + + /** + * 处理 BindingResult + * @param result BindingResult + * @return R + */ + protected R handleError(BindingResult result) { + FieldError error = result.getFieldError(); + String message = String.format("%s:%s", error.getField(), error.getDefaultMessage()); + return R.fail(SystemCode.PARAM_BIND_ERROR, message); + } + + /** + * 处理 ConstraintViolation + * @param violations 校验结果 + * @return R + */ + protected R handleError(Set> violations) { + ConstraintViolation violation = violations.iterator().next(); + String path = ((PathImpl) violation.getPropertyPath()).getLeafNode().getName(); + String message = String.format("%s:%s", path, violation.getMessage()); + return R.fail(SystemCode.PARAM_VALID_ERROR, message); + } +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorEvent.java b/mica-boot/src/main/java/net/dreamlu/mica/common/error/MicaErrorEvent.java similarity index 95% rename from mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorEvent.java rename to mica-boot/src/main/java/net/dreamlu/mica/common/error/MicaErrorEvent.java index eaefe13a7f0475da6503a0f165c98746065e891f..d169382099ca339d46109fec9a1f86292755e838 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorEvent.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/common/error/MicaErrorEvent.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package net.dreamlu.mica.error; +package net.dreamlu.mica.common.error; import lombok.Getter; import lombok.Setter; +import lombok.ToString; import org.springframework.lang.Nullable; import java.time.LocalDateTime; @@ -29,6 +30,7 @@ import java.time.LocalDateTime; */ @Getter @Setter +@ToString public class MicaErrorEvent { /** * 应用名 diff --git a/mica-boot/src/main/java/net/dreamlu/mica/support/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/common/error/package-info.java similarity index 95% rename from mica-boot/src/main/java/net/dreamlu/mica/support/package-info.java rename to mica-boot/src/main/java/net/dreamlu/mica/common/error/package-info.java index 933946318733070c3c8c9c7e9986a8e59a77ba38..bec579fcc4bc4a1f5d50f3705f7dd671412deeaf 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/support/package-info.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/common/error/package-info.java @@ -16,7 +16,7 @@ @NonNullApi @NonNullFields -package net.dreamlu.mica.support; +package net.dreamlu.mica.common.error; import org.springframework.lang.NonNullApi; import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/version/MicaMediaType.java b/mica-boot/src/main/java/net/dreamlu/mica/common/version/MicaMediaType.java similarity index 96% rename from mica-boot/src/main/java/net/dreamlu/mica/version/MicaMediaType.java rename to mica-boot/src/main/java/net/dreamlu/mica/common/version/MicaMediaType.java index 88f5c92b73f3158891247dc7c5375d9593a3616b..6cf19f14dc9e370f18748fc4713dedf7e9ea69fe 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/version/MicaMediaType.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/common/version/MicaMediaType.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package net.dreamlu.mica.version; +package net.dreamlu.mica.common.version; import lombok.Getter; import org.springframework.http.MediaType; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/version/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/common/version/package-info.java similarity index 94% rename from mica-boot/src/main/java/net/dreamlu/mica/version/package-info.java rename to mica-boot/src/main/java/net/dreamlu/mica/common/version/package-info.java index 5965503c58080905a93586aa737b9fc282f21d82..7dcf89d3b09f3f93ca8d3c19c9f44ba604ed8ea7 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/version/package-info.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/common/version/package-info.java @@ -16,7 +16,7 @@ @NonNullApi @NonNullFields -package net.dreamlu.mica.version; +package net.dreamlu.mica.common.version; import org.springframework.lang.NonNullApi; import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/async/MicaExecutorConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/config/MicaExecutorConfiguration.java similarity index 98% rename from mica-boot/src/main/java/net/dreamlu/mica/async/MicaExecutorConfiguration.java rename to mica-boot/src/main/java/net/dreamlu/mica/config/MicaExecutorConfiguration.java index 789f77122005ca595559f27901cfc3e6761fe43c..daa20cacfc9f63203350ddf8334aeba5d6cce2d6 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/async/MicaExecutorConfiguration.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/config/MicaExecutorConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package net.dreamlu.mica.async; +package net.dreamlu.mica.config; import lombok.AllArgsConstructor; import net.dreamlu.mica.props.MicaAsyncProperties; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaConverterConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaConverterConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..c61916fb664bd9402c80efbe99c68785fcd15515 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaConverterConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.config; + +import net.dreamlu.mica.core.convert.EnumToStringConverter; +import net.dreamlu.mica.core.convert.StringToEnumConverter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.format.FormatterRegistry; +import org.springframework.web.reactive.config.WebFluxConfigurer; + +/** + * mica enum 《-》 String 转换配置 + * + * @author L.cm + */ +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class MicaConverterConfiguration implements WebFluxConfigurer { + + @Override + public void addFormatters(FormatterRegistry registry) { + registry.addConverter(new EnumToStringConverter()); + registry.addConverter(new StringToEnumConverter()); + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/version/VersionMappingAutoConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaMessageConfiguration.java similarity index 63% rename from mica-boot/src/main/java/net/dreamlu/mica/version/VersionMappingAutoConfiguration.java rename to mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaMessageConfiguration.java index 911d26406753ebc21e6ccc8f6b8a294c6752d2da..a47cccc9c2e600042054642de19617b01ee734e3 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/version/VersionMappingAutoConfiguration.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaMessageConfiguration.java @@ -14,26 +14,25 @@ * limitations under the License. */ -package net.dreamlu.mica.version; +package net.dreamlu.mica.reactive.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.web.reactive.config.WebFluxConfigurer; /** - * url版本号处理 - *

- * 参考:https://gitee.com/lianqu1990/spring-boot-starter-version-mapping + * 消息转换器 * * @author L.cm */ @Configuration -@ConditionalOnWebApplication -public class VersionMappingAutoConfiguration { +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class MicaMessageConfiguration implements WebFluxConfigurer { - @Bean - public WebMvcRegistrations webMvcRegistrations() { - return new MicaWebMvcRegistrations(); + @Override + public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { + // TODO L.cm json 将 null 转为 空,需求暂时不支持 } + } diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaUploadConfigurtion.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaUploadConfigurtion.java new file mode 100644 index 0000000000000000000000000000000000000000..286ba5e5bdbcbe884a3d5a4b33400d6213ebe63c --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaUploadConfigurtion.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.config; + +import lombok.AllArgsConstructor; +import net.dreamlu.mica.props.MicaUploadProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.config.ResourceHandlerRegistry; +import org.springframework.web.reactive.config.WebFluxConfigurer; + +/** + * 文件上传配置 + * + * @author L.cm + */ +@Configuration +@AllArgsConstructor +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class MicaUploadConfigurtion implements WebFluxConfigurer { + private final MicaUploadProperties properties; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + String path = properties.getSavePath(); + registry.addResourceHandler(properties.getUploadPathPattern()) + .addResourceLocations("file:" + path + "/upload/"); + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaWebFluxRegistrations.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaWebFluxRegistrations.java new file mode 100644 index 0000000000000000000000000000000000000000..c07a8c06d7dff0377fc48e43d4b6b4a8d32d6b1c --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/MicaWebFluxRegistrations.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.config; + +import net.dreamlu.mica.reactive.version.MicaRequestMappingHandlerMapping; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.web.reactive.WebFluxRegistrations; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; + +/** + * webflux 配置 注入 + * + * @author L.cm + */ +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class MicaWebFluxRegistrations implements WebFluxRegistrations { + + @Override + public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { + return new MicaRequestMappingHandlerMapping(); + } +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..6b9db863b859a83f2950c007fb668f5e98238905 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/config/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NonNullApi +@NonNullFields +package net.dreamlu.mica.reactive.config; + +import org.springframework.lang.NonNullApi; +import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaErrorAutoConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaErrorAutoConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..431b60621bd0f202ffba228f8fe20f7c83c4a4b6 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaErrorAutoConfiguration.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.error; + +import lombok.RequiredArgsConstructor; +import net.dreamlu.mica.servlet.error.MicaErrorAttributes; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.autoconfigure.web.ResourceProperties; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.reactive.error.DefaultErrorAttributes; +import org.springframework.boot.web.reactive.error.ErrorAttributes; +import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.web.reactive.result.view.ViewResolver; + +import java.util.List; + +/** + * ExceptionTranslator 只支持控制器中的异常 + * + * @see ErrorWebFluxAutoConfiguration + * @author L.cm + */ +@Configuration +@RequiredArgsConstructor +@AutoConfigureBefore(ErrorWebFluxAutoConfiguration.class) +@EnableConfigurationProperties({ ServerProperties.class, ResourceProperties.class }) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class MicaErrorAutoConfiguration { + + private final ServerProperties serverProperties; + private final ApplicationContext applicationContext; + private final ResourceProperties resourceProperties; + private final ServerCodecConfigurer serverCodecConfigurer; + + @Bean + @Order(-1) + public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) { + MicaErrorWebExceptionHandler exceptionHandler = new MicaErrorWebExceptionHandler( + errorAttributes, this.resourceProperties, this.serverProperties.getError(), this.applicationContext); + exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters()); + exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders()); + return exceptionHandler; + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaErrorWebExceptionHandler.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaErrorWebExceptionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..612600e345a5db5becace17f94ea4feccf2c50fa --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaErrorWebExceptionHandler.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.error; + +import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.core.result.R; +import net.dreamlu.mica.core.result.SystemCode; +import net.dreamlu.mica.core.utils.BeanUtil; +import org.springframework.boot.autoconfigure.web.ErrorProperties; +import org.springframework.boot.autoconfigure.web.ResourceProperties; +import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler; +import org.springframework.boot.web.reactive.error.ErrorAttributes; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.support.WebExchangeBindException; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.server.*; +import org.springframework.web.server.ResponseStatusException; +import reactor.core.publisher.Mono; + +import java.util.Map; + +/** + * webflux 异常处理 + * + * @author L.cm + */ +@Slf4j +public class MicaErrorWebExceptionHandler extends DefaultErrorWebExceptionHandler { + + public MicaErrorWebExceptionHandler(ErrorAttributes attributes, ResourceProperties properties, + ErrorProperties errorProperties, ApplicationContext applicationContext) { + super(attributes, properties, errorProperties, applicationContext); + } + + @Override + protected RouterFunction getRoutingFunction(ErrorAttributes errorAttributes) { + return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); + } + + /** + * Render the error information as a JSON payload. + * @param request the current request + * @return a {@code Publisher} of the HTTP response + */ + @Override + protected Mono renderErrorResponse(ServerRequest request) { + Throwable error = this.getError(request); + HttpStatus status = determineHttpStatus(error); + // 返回消息 + String message = status.value() + ":" + status.getReasonPhrase(); + R result = R.fail(SystemCode.FAILURE, message); + return ServerResponse.status(status) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .body(BodyInserters.fromObject(result)); + } + + private HttpStatus determineHttpStatus(Throwable error) { + if (error instanceof ResponseStatusException) { + return ((ResponseStatusException)error).getStatus(); + } else { + ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(error.getClass(), ResponseStatus.class); + return responseStatus != null ? responseStatus.code() : HttpStatus.INTERNAL_SERVER_ERROR; + } + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaExceptionTranslator.java new file mode 100644 index 0000000000000000000000000000000000000000..ad51025afbedc1d3399ebfd58166c2b657644afb --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/MicaExceptionTranslator.java @@ -0,0 +1,83 @@ +package net.dreamlu.mica.reactive.error; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.core.exception.ServiceException; +import net.dreamlu.mica.core.result.R; +import net.dreamlu.mica.core.result.SystemCode; +import net.dreamlu.mica.core.utils.Exceptions; +import net.dreamlu.mica.core.utils.ObjectUtil; +import net.dreamlu.mica.reactive.filter.ReactiveRequestContextHolder; +import net.dreamlu.mica.common.error.MicaErrorEvent; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import reactor.core.publisher.Mono; + +import java.time.LocalDateTime; + +/** + * mica 未知异常转译和发送,方便监听,对未知异常统一处理。Order 排序优先级低 + * + * @author L.cm + */ +@Slf4j +@Order +@Configuration +@RestControllerAdvice +@AllArgsConstructor +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class MicaExceptionTranslator { + private final ApplicationEventPublisher publisher; + + @ExceptionHandler(ServiceException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public Mono> handleError(ServiceException e) { + log.error("业务异常", e); + R result = e.getResult(); + if (result != null) { + return Mono.just(result); + } + // 发送:未知异常异常事件 + return ReactiveRequestContextHolder.getRequest() + .doOnSuccess(r -> publishEvent(r, e)) + .flatMap(r -> Mono.just(R.fail(SystemCode.FAILURE, e.getMessage()))); + } + + @ExceptionHandler(Throwable.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public Mono handleError(Throwable e) { + log.error("未知异常", e); + // 发送:未知异常异常事件 + return ReactiveRequestContextHolder.getRequest() + .doOnSuccess(r -> publishEvent(r, e)) + .flatMap(r -> Mono.just(R.fail(SystemCode.FAILURE))); + } + + private void publishEvent(ServerHttpRequest request, Throwable error) { + String requestUrl = request.getPath().pathWithinApplication().value(); + MicaErrorEvent event = new MicaErrorEvent(); + event.setRequestUrl(requestUrl); + event.setStackTrace(Exceptions.getStackTraceAsString(error)); + event.setExceptionName(error.getClass().getName()); + event.setMessage(error.getMessage()); + event.setCreatedAt(LocalDateTime.now()); + StackTraceElement[] elements = error.getStackTrace(); + if (ObjectUtil.isNotEmpty(elements)) { + StackTraceElement element = elements[0]; + event.setClassName(element.getClassName()); + event.setFileName(element.getFileName()); + event.setMethodName(element.getMethodName()); + event.setLineNumber(element.getLineNumber()); + } + // 发布事件 + publisher.publishEvent(event); + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/RestExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/RestExceptionTranslator.java new file mode 100644 index 0000000000000000000000000000000000000000..a8892d404667614a6679e34881b6d9f9e9d97566 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/RestExceptionTranslator.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.error; + +import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.common.error.BaseExceptionTranslator; +import net.dreamlu.mica.core.result.R; +import net.dreamlu.mica.core.result.SystemCode; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.MethodParameter; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindException; +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.bind.support.WebExchangeBindException; +import org.springframework.web.server.ResponseStatusException; +import org.springframework.web.server.ServerWebInputException; +import reactor.core.publisher.Mono; + +import javax.validation.ConstraintViolationException; + +/** + * 异常信息处理 + */ +@Slf4j +@Order(Ordered.HIGHEST_PRECEDENCE) +@Configuration +@RestControllerAdvice +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class RestExceptionTranslator extends BaseExceptionTranslator { + + @ExceptionHandler(ServerWebInputException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleError(ServerWebInputException e) { + log.error("缺少请求参数:{}", e.getMessage()); + MethodParameter parameter = e.getMethodParameter(); + String message = String.format("缺少必要的请求参数: %s", parameter.getParameterName()); + return Mono.just(R.fail(SystemCode.PARAM_MISS, message)); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleError(MethodArgumentNotValidException e) { + log.error("参数验证失败:{}", e.getMessage()); + return Mono.just(handleError(e.getBindingResult())); + } + + @ExceptionHandler(BindException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleError(BindException e) { + log.error("参数绑定失败:{}", e.getMessage()); + return Mono.just(handleError(e.getBindingResult())); + } + + @ExceptionHandler(WebExchangeBindException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleError(WebExchangeBindException e) { + log.error("参数绑定失败:{}", e.getMessage()); + return Mono.just(handleError(e.getBindingResult())); + } + + @ExceptionHandler(ConstraintViolationException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Mono> handleError(ConstraintViolationException e) { + log.error("参数验证失败:{}", e.getMessage()); + return Mono.just(handleError(e.getConstraintViolations())); + } + + @ExceptionHandler(ResponseStatusException.class) + public Mono>> handleError(ResponseStatusException e) { + log.error("响应状态异常:{}", e.getMessage()); + ResponseEntity> entity = ResponseEntity.status(e.getStatus()) + .body(R.fail(SystemCode.REQ_REJECT, e.getMessage())); + return Mono.just(entity); + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/package-info.java similarity index 94% rename from mica-boot/src/main/java/net/dreamlu/mica/error/package-info.java rename to mica-boot/src/main/java/net/dreamlu/mica/reactive/error/package-info.java index 1d0786a34f89d2f6bf5c43ffd70e25a67fa291b9..4abd82ac9d642df97acc363402dd0c30d563e299 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/package-info.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/error/package-info.java @@ -16,7 +16,7 @@ @NonNullApi @NonNullFields -package net.dreamlu.mica.error; +package net.dreamlu.mica.reactive.error; import org.springframework.lang.NonNullApi; import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/ReactiveRequestContextFilter.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/ReactiveRequestContextFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..4c3c7b3346211e36e09e17323fd0c6f153e185c4 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/ReactiveRequestContextFilter.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.filter; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +/** + * ReactiveRequestContextFilter + * + * @author L.cm + */ +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +public class ReactiveRequestContextFilter implements WebFilter { + + @Override + public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { + ServerHttpRequest request = exchange.getRequest(); + return chain.filter(exchange).subscriberContext(ctx -> ctx.put(ReactiveRequestContextHolder.CONTEXT_KEY, request)); + } +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/ReactiveRequestContextHolder.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/ReactiveRequestContextHolder.java new file mode 100644 index 0000000000000000000000000000000000000000..9d7a90d189382dd23bdc4275b0256fc1701b0059 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/ReactiveRequestContextHolder.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.filter; + +import org.springframework.http.server.reactive.ServerHttpRequest; +import reactor.core.publisher.Mono; +import reactor.util.context.Context; + +/** + * ReactiveRequestContextHolder + * + * @author L.cm + */ +public class ReactiveRequestContextHolder { + static final Class CONTEXT_KEY = ServerHttpRequest.class; + + /** + * Gets the {@code Mono} from Reactor {@link Context} + * @return the {@code Mono} + */ + public static Mono getRequest() { + return Mono.subscriberContext() + .map(ctx -> ctx.get(CONTEXT_KEY)) + .cast(ServerHttpRequest.class); + } + +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..9c76005a5656cca4cb3b8483bb92f9a79c640d60 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/filter/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NonNullApi +@NonNullFields +package net.dreamlu.mica.reactive.filter; + +import org.springframework.lang.NonNullApi; +import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/version/MicaRequestMappingHandlerMapping.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/version/MicaRequestMappingHandlerMapping.java new file mode 100644 index 0000000000000000000000000000000000000000..f6de4f5842d66514a6f248bb03c86a32ba5ca723 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/version/MicaRequestMappingHandlerMapping.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dreamlu.mica.reactive.version; + +import net.dreamlu.mica.annotation.ApiVersion; +import net.dreamlu.mica.annotation.UrlVersion; +import net.dreamlu.mica.common.version.MicaMediaType; +import net.dreamlu.mica.core.utils.StringPool; +import net.dreamlu.mica.core.utils.StringUtil; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.lang.Nullable; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.reactive.result.method.RequestMappingInfo; +import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; + +import java.lang.reflect.Method; +import java.util.Map; + +/** + * url版本号处理 + * + * @author L.cm + */ +public class MicaRequestMappingHandlerMapping extends RequestMappingHandlerMapping { + + @Override + protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) { + RequestMappingInfo mappinginfo = super.getMappingForMethod(method, handlerType); + if (mappinginfo != null) { + RequestMappingInfo apiVersionMappingInfo = getApiVersionMappingInfo(method, handlerType); + return apiVersionMappingInfo == null ? mappinginfo : apiVersionMappingInfo.combine(mappinginfo); + } + return mappinginfo; + } + + @Nullable + private RequestMappingInfo getApiVersionMappingInfo(Method method, Class handlerType) { + // url 上的版本,优先获取方法上的版本 + UrlVersion urlVersion = AnnotatedElementUtils.findMergedAnnotation(method, UrlVersion.class); + // 再次尝试类上的版本 + if (urlVersion == null || StringUtil.isBlank(urlVersion.value())) { + urlVersion = AnnotatedElementUtils.findMergedAnnotation(handlerType, UrlVersion.class); + } + // Media Types 版本信息 + ApiVersion apiVersion = AnnotatedElementUtils.findMergedAnnotation(method, ApiVersion.class); + // 再次尝试类上的版本 + if (apiVersion == null || StringUtil.isBlank(apiVersion.value())) { + apiVersion = AnnotatedElementUtils.findMergedAnnotation(handlerType, ApiVersion.class); + } + boolean nonUrlVersion = urlVersion == null || StringUtil.isBlank(urlVersion.value()); + boolean nonApiVersion = apiVersion == null || StringUtil.isBlank(apiVersion.value()); + // 先判断同时不纯在 + if (nonUrlVersion && nonApiVersion) { + return null; + } + // 如果 header 版本不存在 + RequestMappingInfo.Builder mappingInfoBuilder; + if (nonApiVersion) { + mappingInfoBuilder = RequestMappingInfo.paths(urlVersion.value()); + } else { + mappingInfoBuilder = RequestMappingInfo.paths(StringPool.EMPTY); + } + // 如果url版本不存在 + if (nonUrlVersion) { + String vsersionMediaTypes = new MicaMediaType(apiVersion.value()).toString(); + mappingInfoBuilder.produces(vsersionMediaTypes); + } + return mappingInfoBuilder.build(); + } + + @Override + protected void handlerMethodsInitialized(Map handlerMethods) { + // 打印路由信息 spring boot 2.1 去掉了这个 日志的打印 + if (logger.isInfoEnabled()) { + for (Map.Entry entry : handlerMethods.entrySet()) { + RequestMappingInfo mapping = entry.getKey(); + HandlerMethod handlerMethod = entry.getValue(); + logger.info("Mapped \"" + mapping + "\" onto " + handlerMethod); + } + } + super.handlerMethodsInitialized(handlerMethods); + } +} diff --git a/mica-boot/src/main/java/net/dreamlu/mica/reactive/version/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/reactive/version/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..48438c7b05c05b9d17b843e28e12a9ba30e6d9b0 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/reactive/version/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NonNullApi +@NonNullFields +package net.dreamlu.mica.reactive.version; + +import org.springframework.lang.NonNullApi; +import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/config/MicaConverterConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaConverterConfiguration.java similarity index 86% rename from mica-boot/src/main/java/net/dreamlu/mica/config/MicaConverterConfiguration.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaConverterConfiguration.java index 209f6383806541a0ee628045741e290e2b691973..fc926ef968ed6d9457b0c0180747a10b474e091b 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/config/MicaConverterConfiguration.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaConverterConfiguration.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package net.dreamlu.mica.config; +package net.dreamlu.mica.servlet.config; import net.dreamlu.mica.core.convert.EnumToStringConverter; import net.dreamlu.mica.core.convert.StringToEnumConverter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -28,6 +29,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; * @author L.cm */ @Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class MicaConverterConfiguration implements WebMvcConfigurer { @Override diff --git a/mica-boot/src/main/java/net/dreamlu/mica/config/MicaMessageConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaMessageConfiguration.java similarity index 91% rename from mica-boot/src/main/java/net/dreamlu/mica/config/MicaMessageConfiguration.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaMessageConfiguration.java index 480fdada8c2a71e35c7cdd3abf94ab9e2f3b2ba6..2acaa66058df65ed70e7975ceb4742050731c4cb 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/config/MicaMessageConfiguration.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaMessageConfiguration.java @@ -14,12 +14,13 @@ * limitations under the License. */ -package net.dreamlu.mica.config; +package net.dreamlu.mica.servlet.config; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.AllArgsConstructor; import net.dreamlu.mica.core.jackson.MappingApiJackson2HttpMessageConverter; import net.dreamlu.mica.props.MicaJacksonProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.*; import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; @@ -36,6 +37,7 @@ import java.util.List; */ @Configuration @AllArgsConstructor +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class MicaMessageConfiguration implements WebMvcConfigurer { private final MicaJacksonProperties properties; private final ObjectMapper objectMapper; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/config/MicaUploadConfigurtion.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaUploadConfigurtion.java similarity index 87% rename from mica-boot/src/main/java/net/dreamlu/mica/config/MicaUploadConfigurtion.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaUploadConfigurtion.java index 62b0107f53a2cb6aa6359cb1e86c4cb9ab6fff0b..1c0039f2b64ab2268212236c07fd35f46c19c169 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/config/MicaUploadConfigurtion.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaUploadConfigurtion.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package net.dreamlu.mica.config; +package net.dreamlu.mica.servlet.config; import lombok.AllArgsConstructor; import net.dreamlu.mica.props.MicaUploadProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -29,6 +30,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; */ @Configuration @AllArgsConstructor +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class MicaUploadConfigurtion implements WebMvcConfigurer { private final MicaUploadProperties properties; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/version/MicaWebMvcRegistrations.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaWebMvcRegistrations.java similarity index 71% rename from mica-boot/src/main/java/net/dreamlu/mica/version/MicaWebMvcRegistrations.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaWebMvcRegistrations.java index 0bc7a067588ef8df3d04cca0102037fbe2f71410..af1c8b6232668f9000174d9b7ef24ea4387fe770 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/version/MicaWebMvcRegistrations.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/MicaWebMvcRegistrations.java @@ -14,11 +14,12 @@ * limitations under the License. */ -package net.dreamlu.mica.version; +package net.dreamlu.mica.servlet.config; +import net.dreamlu.mica.servlet.version.MicaRequestMappingHandlerMapping; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations; -import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; +import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; /** @@ -26,19 +27,13 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl * * @author L.cm */ +@Configuration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class MicaWebMvcRegistrations implements WebMvcRegistrations { + @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new MicaRequestMappingHandlerMapping(); } - @Override - public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() { - return null; - } - - @Override - public ExceptionHandlerExceptionResolver getExceptionHandlerExceptionResolver() { - return null; - } } diff --git a/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..c6cb70fe75607966f6f08b90fa861f2533fd9d8d --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/config/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NonNullApi +@NonNullFields +package net.dreamlu.mica.servlet.config; + +import org.springframework.lang.NonNullApi; +import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorAttributes.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorAttributes.java similarity index 98% rename from mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorAttributes.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorAttributes.java index a15eac6225388e9a0bf2f9be220124c9851a015f..213bf491e64f4806ec244e7ee65772f5e5b96336 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorAttributes.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorAttributes.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package net.dreamlu.mica.error; +package net.dreamlu.mica.servlet.error; import lombok.extern.slf4j.Slf4j; import net.dreamlu.mica.core.result.R; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorAutoConfiguration.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorAutoConfiguration.java similarity index 95% rename from mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorAutoConfiguration.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorAutoConfiguration.java index 487458006db8a2bd8c5f4fafd327258650beb652..e269fc7bed3e883a6ea1e2b677167434bf737490 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorAutoConfiguration.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorAutoConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package net.dreamlu.mica.error; +package net.dreamlu.mica.servlet.error; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.AllArgsConstructor; @@ -41,10 +41,10 @@ import javax.servlet.Servlet; * @author L.cm */ @Configuration -@ConditionalOnWebApplication -@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) -@AutoConfigureBefore(ErrorMvcAutoConfiguration.class) @AllArgsConstructor +@AutoConfigureBefore(ErrorMvcAutoConfiguration.class) +@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class MicaErrorAutoConfiguration { private final ServerProperties serverProperties; private final ObjectMapper objectMapper; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorController.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorController.java similarity index 98% rename from mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorController.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorController.java index bcce104a019d3f0d32f3e3108fb3fb2fb29eab3d..8b7e587ad27733160e4fda4aa19569d1e47b83d2 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaErrorController.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaErrorController.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package net.dreamlu.mica.error; +package net.dreamlu.mica.servlet.error; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.autoconfigure.web.ErrorProperties; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaExceptionTranslator.java similarity index 89% rename from mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaExceptionTranslator.java index 23a225019ab517a7b8f5ac0888126e030db99bda..e34487f2358068f8fe4b6b1564f788f7f42297de 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/MicaExceptionTranslator.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/MicaExceptionTranslator.java @@ -1,7 +1,8 @@ -package net.dreamlu.mica.error; +package net.dreamlu.mica.servlet.error; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.common.error.MicaErrorEvent; import net.dreamlu.mica.core.exception.ServiceException; import net.dreamlu.mica.core.result.R; import net.dreamlu.mica.core.result.SystemCode; @@ -31,18 +32,18 @@ import java.time.LocalDateTime; @Slf4j @Order @Configuration -@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) -@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) @RestControllerAdvice @AllArgsConstructor +@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class MicaExceptionTranslator { private final ApplicationEventPublisher publisher; @ExceptionHandler(ServiceException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public R handleError(ServiceException e) { + public R handleError(ServiceException e) { log.error("业务异常", e); - R result = e.getResult(); + R result = e.getResult(); if (result == null) { // 发送:未知业务异常事件 result = R.fail(SystemCode.FAILURE, e.getMessage()); @@ -53,7 +54,7 @@ public class MicaExceptionTranslator { @ExceptionHandler(Throwable.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public R handleError(Throwable e) { + public R handleError(Throwable e) { log.error("未知异常", e); // 发送:未知异常异常事件 publishEvent(e); @@ -61,9 +62,10 @@ public class MicaExceptionTranslator { } private void publishEvent(Throwable error) { - HttpServletRequest request = WebUtil.getRequest(); MicaErrorEvent event = new MicaErrorEvent(); - event.setRequestUrl(request.getRequestURI()); + HttpServletRequest request = WebUtil.getRequest(); + String requestUrl = request.getRequestURI() + "?" + request.getQueryString(); + event.setRequestUrl(requestUrl); event.setStackTrace(Exceptions.getStackTraceAsString(error)); event.setExceptionName(error.getClass().getName()); event.setMessage(error.getMessage()); diff --git a/mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/RestExceptionTranslator.java similarity index 69% rename from mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/error/RestExceptionTranslator.java index 26b75fee4e741dacccf5152fc744621c5e39d64e..fe6d99c4023b7a45926b0f49c1f6522d97bbaea1 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/error/RestExceptionTranslator.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/RestExceptionTranslator.java @@ -14,14 +14,13 @@ * limitations under the License. */ -package net.dreamlu.mica.error; +package net.dreamlu.mica.servlet.error; import lombok.extern.slf4j.Slf4j; +import net.dreamlu.mica.common.error.BaseExceptionTranslator; import net.dreamlu.mica.core.result.R; import net.dreamlu.mica.core.result.SystemCode; import net.dreamlu.mica.core.utils.StringUtil; -import org.hibernate.validator.internal.engine.path.PathImpl; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; @@ -29,8 +28,6 @@ import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; 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.HttpMediaTypeNotAcceptableException; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException; @@ -40,13 +37,9 @@ 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.DispatcherServlet; import org.springframework.web.servlet.NoHandlerFoundException; -import javax.servlet.Servlet; -import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; -import java.util.Set; /** * 全局异常处理,处理可预见的异常,Order 排序优先级高 @@ -60,89 +53,78 @@ import java.util.Set; @Slf4j @Order(Ordered.HIGHEST_PRECEDENCE) @Configuration -@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) -@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) @RestControllerAdvice -public class RestExceptionTranslator { +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class RestExceptionTranslator extends BaseExceptionTranslator { @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public R handleError(MissingServletRequestParameterException e) { - log.warn("缺少请求参数", e.getMessage()); + public R handleError(MissingServletRequestParameterException e) { + log.warn("缺少请求参数:{}", e.getMessage()); String message = String.format("缺少必要的请求参数: %s", e.getParameterName()); return R.fail(SystemCode.PARAM_MISS, message); } @ExceptionHandler(MethodArgumentTypeMismatchException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public R handleError(MethodArgumentTypeMismatchException e) { - log.warn("请求参数格式错误", e.getMessage()); + public R handleError(MethodArgumentTypeMismatchException e) { + log.warn("请求参数格式错误:{}", e.getMessage()); String message = String.format("请求参数格式错误: %s", e.getName()); return R.fail(SystemCode.PARAM_TYPE_ERROR, message); } @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public R handleError(MethodArgumentNotValidException e) { - log.warn("参数验证失败", e.getMessage()); + public R handleError(MethodArgumentNotValidException e) { + log.warn("参数验证失败:{}", e.getMessage()); return handleError(e.getBindingResult()); } @ExceptionHandler(BindException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public R handleError(BindException e) { - log.warn("参数绑定失败", e.getMessage()); + public R handleError(BindException e) { + log.warn("参数绑定失败:{}", e.getMessage()); return handleError(e.getBindingResult()); } - private R handleError(BindingResult result) { - FieldError error = result.getFieldError(); - String message = String.format("%s:%s", error.getField(), error.getDefaultMessage()); - return R.fail(SystemCode.PARAM_BIND_ERROR, message); - } - @ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public R handleError(ConstraintViolationException e) { - log.warn("参数验证失败", e.getLocalizedMessage()); - Set> violations = e.getConstraintViolations(); - ConstraintViolation violation = violations.iterator().next(); - String path = ((PathImpl) violation.getPropertyPath()).getLeafNode().getName(); - String message = String.format("%s:%s", path, violation.getMessage()); - return R.fail(SystemCode.PARAM_VALID_ERROR, message); + public R handleError(ConstraintViolationException e) { + log.warn("参数验证失败:{}", e.getMessage()); + return handleError(e.getConstraintViolations()); } @ExceptionHandler(NoHandlerFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) - public R handleError(NoHandlerFoundException e) { + public R handleError(NoHandlerFoundException e) { log.error("404没找到请求:{}", e.getMessage()); return R.fail(SystemCode.NOT_FOUND, e.getMessage()); } @ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public R handleError(HttpMessageNotReadableException e) { + public R handleError(HttpMessageNotReadableException e) { log.error("消息不能读取:{}", e.getMessage()); return R.fail(SystemCode.MSG_NOT_READABLE, e.getMessage()); } @ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) - public R handleError(HttpRequestMethodNotSupportedException e) { + public R handleError(HttpRequestMethodNotSupportedException e) { log.error("不支持当前请求方法:{}", e.getMessage()); return R.fail(SystemCode.METHOD_NOT_SUPPORTED, e.getMessage()); } @ExceptionHandler(HttpMediaTypeNotSupportedException.class) @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) - public R handleError(HttpMediaTypeNotSupportedException e) { + public R handleError(HttpMediaTypeNotSupportedException e) { log.error("不支持当前媒体类型:{}", e.getMessage()); return R.fail(SystemCode.MEDIA_TYPE_NOT_SUPPORTED, e.getMessage()); } @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) - public R handleError(HttpMediaTypeNotAcceptableException e) { + public R handleError(HttpMediaTypeNotAcceptableException e) { String message = e.getMessage() + " " + StringUtil.join(e.getSupportedMediaTypes()); log.error("不接受的媒体类型:{}", message); return R.fail(SystemCode.MEDIA_TYPE_NOT_SUPPORTED, message); diff --git a/mica-boot/src/main/java/net/dreamlu/mica/async/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/package-info.java similarity index 95% rename from mica-boot/src/main/java/net/dreamlu/mica/async/package-info.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/error/package-info.java index ee1d20f6aa9e74b92f512c93f7191abe23e7bbf9..85e6627ccdb92599d1d4cb7f414243a133fbe7d0 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/async/package-info.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/error/package-info.java @@ -16,7 +16,7 @@ @NonNullApi @NonNullFields -package net.dreamlu.mica.async; +package net.dreamlu.mica.servlet.error; import org.springframework.lang.NonNullApi; import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/support/BaseController.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/support/BaseController.java similarity index 99% rename from mica-boot/src/main/java/net/dreamlu/mica/support/BaseController.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/support/BaseController.java index 3759f1ea12f3efc7912df47619bb29cc68937c93..5634c10727666793322c7ca1b176e18ae7381763 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/support/BaseController.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/support/BaseController.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package net.dreamlu.mica.support; +package net.dreamlu.mica.servlet.support; import net.dreamlu.mica.core.result.IResultCode; import net.dreamlu.mica.core.result.R; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/servlet/support/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/support/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..b6870f05671f845e0edff6d8b0348759db84f4e5 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/support/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NonNullApi +@NonNullFields +package net.dreamlu.mica.servlet.support; + +import org.springframework.lang.NonNullApi; +import org.springframework.lang.NonNullFields; diff --git a/mica-boot/src/main/java/net/dreamlu/mica/version/MicaRequestMappingHandlerMapping.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/version/MicaRequestMappingHandlerMapping.java similarity index 96% rename from mica-boot/src/main/java/net/dreamlu/mica/version/MicaRequestMappingHandlerMapping.java rename to mica-boot/src/main/java/net/dreamlu/mica/servlet/version/MicaRequestMappingHandlerMapping.java index 8032a2b8affb893130bd9a323f0746c9882abb90..fc385a0813d3daa6e2a7f401513cb58f947b148d 100644 --- a/mica-boot/src/main/java/net/dreamlu/mica/version/MicaRequestMappingHandlerMapping.java +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/version/MicaRequestMappingHandlerMapping.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package net.dreamlu.mica.version; +package net.dreamlu.mica.servlet.version; import net.dreamlu.mica.annotation.ApiVersion; import net.dreamlu.mica.annotation.UrlVersion; +import net.dreamlu.mica.common.version.MicaMediaType; import net.dreamlu.mica.core.utils.StringPool; import net.dreamlu.mica.core.utils.StringUtil; import org.springframework.core.annotation.AnnotatedElementUtils; @@ -75,7 +76,7 @@ public class MicaRequestMappingHandlerMapping extends RequestMappingHandlerMappi return null; } // 如果 header 版本不存在 - RequestMappingInfo.Builder mappingInfoBuilder = null; + RequestMappingInfo.Builder mappingInfoBuilder; if (nonApiVersion) { mappingInfoBuilder = RequestMappingInfo.paths(urlVersion.value()); } else { diff --git a/mica-boot/src/main/java/net/dreamlu/mica/servlet/version/package-info.java b/mica-boot/src/main/java/net/dreamlu/mica/servlet/version/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..41d20e7696a8d301b4e4615bba4c0951766e29f2 --- /dev/null +++ b/mica-boot/src/main/java/net/dreamlu/mica/servlet/version/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NonNullApi +@NonNullFields +package net.dreamlu.mica.servlet.version; + +import org.springframework.lang.NonNullApi; +import org.springframework.lang.NonNullFields; diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/exception/ServiceException.java b/mica-core/src/main/java/net/dreamlu/mica/core/exception/ServiceException.java index 2114d7db2a880f27b826c8a6e887491899017330..d67f0765b05fa5973cb34f8678b71d16a9d6eec4 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/exception/ServiceException.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/exception/ServiceException.java @@ -32,9 +32,9 @@ public class ServiceException extends RuntimeException { @Getter @Nullable - private final R result; + private final R result; - public ServiceException(R result) { + public ServiceException(R result) { super(result.getMsg()); this.result = result; } diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java index f215313687c6bcb5820f4051c11e590d6a067e63..47b64e68e9827cf8da68d84361569b3c8bce3178 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/NumberUtil.java @@ -153,7 +153,7 @@ public class NumberUtil extends org.springframework.util.NumberUtils { i = i / radix; } buf[charPos] = DIGITS[(int)(-i)]; - return new String(buf, charPos, (65 - charPos)); + return new String(buf, charPos, (65 - charPos), Charsets.UTF_8); } } diff --git a/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java b/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java index 450a005316b46eb54c2925c4c1eb239b0d59f57e..9ac80fa1bcf0b6f54c2a1c166feeb928e4dfad57 100644 --- a/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java +++ b/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java @@ -322,7 +322,7 @@ public class StringUtil extends org.springframework.util.StringUtils { formatUnsignedLong0(msb, buf, 12, 4); formatUnsignedLong0(msb >>> 16, buf, 8, 4); formatUnsignedLong0(msb >>> 32, buf, 0, 8); - return new String(buf); + return new String(buf, Charsets.UTF_8); } private static void formatUnsignedLong0(long val, byte[] buf, int offset, int len) { diff --git a/mica-launcher/src/main/java/net/dreamlu/mica/launcher/MicaApplication.java b/mica-launcher/src/main/java/net/dreamlu/mica/launcher/MicaApplication.java index 0607cd5a83894cc7bd1dfe575b3f7eed44265715..0eaafe62b84612018200ba9c5527dc924b9fa6a9 100644 --- a/mica-launcher/src/main/java/net/dreamlu/mica/launcher/MicaApplication.java +++ b/mica-launcher/src/main/java/net/dreamlu/mica/launcher/MicaApplication.java @@ -99,7 +99,7 @@ public class MicaApplication { props.setProperty("mica.env", profile); props.setProperty("mica.is-local", String.valueOf(isLocalDev)); props.setProperty("spring.application.name", appName); - props.setProperty("spring.banner.location", "classpath:mica_banner.txt"); + props.setProperty("spring.banner.location", "classpath:banner.txt"); // 加载自定义组件 ServiceLoader loader = ServiceLoader.load(LauncherService.class); // 启动组件 diff --git a/mica-launcher/src/main/resources/mica_banner.txt b/mica-launcher/src/main/resources/banner.txt similarity index 100% rename from mica-launcher/src/main/resources/mica_banner.txt rename to mica-launcher/src/main/resources/banner.txt