feat(extension/crud): 新增启用注解,便于灵活控制启用/关闭 CRUD REST API、全局异常处理器增强

This commit is contained in:
2024-01-17 20:47:23 +08:00
parent 926c92cc32
commit 9398d686bb
5 changed files with 83 additions and 11 deletions

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 top.charles7c.continew.starter.extension.crud.annotation;
import org.springframework.context.annotation.Import;
import top.charles7c.continew.starter.extension.crud.autoconfigure.CrudRestControllerAutoConfiguration;
import java.lang.annotation.*;
/**
* CRUD REST Controller 启用注解
*
* @author Charles7c
* @since 1.2.0
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({CrudRestControllerAutoConfiguration.class})
public @interface EnableCrudRestController {
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 top.charles7c.continew.starter.extension.crud.annotation;
import org.springframework.context.annotation.Import;
import top.charles7c.continew.starter.extension.crud.autoconfigure.GlobalExceptionHandlerAutoConfiguration;
import java.lang.annotation.*;
/**
* 全局异常、错误处理器启用注解
*
* @author Charles7c
* @since 1.2.0
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({GlobalExceptionHandlerAutoConfiguration.class})
public @interface EnableGlobalExceptionHandler {
}

View File

@@ -19,8 +19,8 @@ package top.charles7c.continew.starter.extension.crud.autoconfigure;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.format.support.FormattingConversionService; import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ContentNegotiationManager;
@@ -30,14 +30,14 @@ import org.springframework.web.servlet.resource.ResourceUrlProvider;
import top.charles7c.continew.starter.extension.crud.handler.CrudRequestMappingHandlerMapping; import top.charles7c.continew.starter.extension.crud.handler.CrudRequestMappingHandlerMapping;
/** /**
* CRUD 自动配置 * CRUD REST Controller 自动配置
* *
* @author Charles7c * @author Charles7c
* @since 1.0.0 * @since 1.0.0
*/ */
@Slf4j @Slf4j
@AutoConfiguration @Configuration
public class CrudAutoConfiguration extends DelegatingWebMvcConfiguration { public class CrudRestControllerAutoConfiguration extends DelegatingWebMvcConfiguration {
/** /**
* CRUD 请求映射器处理器映射器覆盖默认 RequestMappingHandlerMapping * CRUD 请求映射器处理器映射器覆盖默认 RequestMappingHandlerMapping
@@ -58,6 +58,6 @@ public class CrudAutoConfiguration extends DelegatingWebMvcConfiguration {
@PostConstruct @PostConstruct
public void postConstruct() { public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Extension-CRUD' completed initialization."); log.debug("[ContiNew Starter] - Auto Configuration 'Extension-CRUD REST Controller' completed initialization.");
} }
} }

View File

@@ -16,11 +16,11 @@
package top.charles7c.continew.starter.extension.crud.autoconfigure; package top.charles7c.continew.starter.extension.crud.autoconfigure;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import top.charles7c.continew.starter.extension.crud.handler.GlobalErrorHandler; import top.charles7c.continew.starter.extension.crud.handler.GlobalErrorHandler;
import top.charles7c.continew.starter.extension.crud.handler.GlobalExceptionHandler; import top.charles7c.continew.starter.extension.crud.handler.GlobalExceptionHandler;
@@ -32,9 +32,13 @@ import top.charles7c.continew.starter.extension.crud.handler.GlobalExceptionHand
* @since 1.0.0 * @since 1.0.0
*/ */
@Slf4j @Slf4j
@AutoConfiguration @Configuration(proxyBeanMethods = false)
@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class}) @Import({GlobalExceptionHandler.class, GlobalErrorHandler.class})
@ConditionalOnMissingBean(BasicErrorController.class) @ConditionalOnMissingBean(BasicErrorController.class)
@ComponentScan("top.charles7c.continew.starter.extension.crud.handler")
public class GlobalExceptionHandlerAutoConfiguration { public class GlobalExceptionHandlerAutoConfiguration {
@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Extension-Global Exception Handler' completed " + "initialization.");
}
} }

View File

@@ -1,2 +0,0 @@
top.charles7c.continew.starter.extension.crud.autoconfigure.CrudAutoConfiguration
top.charles7c.continew.starter.extension.crud.autoconfigure.GlobalExceptionHandlerAutoConfiguration