mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 22:57:19 +08:00 
			
		
		
		
	feat(web): 新增 BaseEnum 枚举接口参数转换器
This commit is contained in:
		| @@ -0,0 +1,49 @@ | |||||||
|  | /* | ||||||
|  |  * 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.continew.starter.web.autoconfigure.converter; | ||||||
|  |  | ||||||
|  | import org.springframework.core.convert.converter.Converter; | ||||||
|  | import top.continew.starter.core.enums.BaseEnum; | ||||||
|  | import top.continew.starter.core.util.validate.ValidationUtils; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * BaseEnum 参数转换器 | ||||||
|  |  * | ||||||
|  |  * @author Charles7c | ||||||
|  |  * @since 2.4.0 | ||||||
|  |  */ | ||||||
|  | public class BaseEnumConverter<T extends BaseEnum> implements Converter<String, T> { | ||||||
|  |  | ||||||
|  |     private final Map<String, T> enumMap = new HashMap<>(); | ||||||
|  |  | ||||||
|  |     public BaseEnumConverter(Class<T> enumType) { | ||||||
|  |         T[] enums = enumType.getEnumConstants(); | ||||||
|  |         for (T e : enums) { | ||||||
|  |             enumMap.put(String.valueOf(e.getValue()), e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public T convert(String source) { | ||||||
|  |         T t = enumMap.get(source); | ||||||
|  |         ValidationUtils.throwIfNull(t, "枚举值非法:{}", source); | ||||||
|  |         return t; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,48 @@ | |||||||
|  | /* | ||||||
|  |  * 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.continew.starter.web.autoconfigure.converter; | ||||||
|  |  | ||||||
|  | import jakarta.annotation.PostConstruct; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  | import org.springframework.boot.autoconfigure.AutoConfiguration; | ||||||
|  | import org.springframework.format.FormatterRegistry; | ||||||
|  | import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||||||
|  | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * BaseEnum 参数转换自动配置 | ||||||
|  |  * | ||||||
|  |  * @author Charles7c | ||||||
|  |  * @since 2.4.0 | ||||||
|  |  */ | ||||||
|  | @EnableWebMvc | ||||||
|  | @AutoConfiguration | ||||||
|  | public class BaseEnumConverterAutoConfiguration implements WebMvcConfigurer { | ||||||
|  |  | ||||||
|  |     private static final Logger log = LoggerFactory.getLogger(BaseEnumConverterAutoConfiguration.class); | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void addFormatters(FormatterRegistry registry) { | ||||||
|  |         registry.addConverterFactory(new BaseEnumConverterFactory()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @PostConstruct | ||||||
|  |     public void postConstruct() { | ||||||
|  |         log.debug("[ContiNew Starter] - Auto Configuration 'Web-BaseEnum Converter' completed initialization."); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,40 @@ | |||||||
|  | /* | ||||||
|  |  * 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.continew.starter.web.autoconfigure.converter; | ||||||
|  |  | ||||||
|  | import org.springframework.core.convert.converter.Converter; | ||||||
|  | import org.springframework.core.convert.converter.ConverterFactory; | ||||||
|  | import top.continew.starter.core.enums.BaseEnum; | ||||||
|  |  | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.concurrent.ConcurrentHashMap; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * BaseEnum 参数转换器工厂 | ||||||
|  |  * | ||||||
|  |  * @author Charles7c | ||||||
|  |  * @since 2.4.0 | ||||||
|  |  */ | ||||||
|  | public class BaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> { | ||||||
|  |  | ||||||
|  |     private static final Map<Class, Converter> CONVERTER_CACHE = new ConcurrentHashMap<>(); | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) { | ||||||
|  |         return CONVERTER_CACHE.computeIfAbsent(targetType, key -> new BaseEnumConverter<>(targetType)); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | top.continew.starter.web.autoconfigure.converter.BaseEnumConverterAutoConfiguration | ||||||
| top.continew.starter.web.autoconfigure.cors.CorsAutoConfiguration | top.continew.starter.web.autoconfigure.cors.CorsAutoConfiguration | ||||||
| top.continew.starter.web.autoconfigure.trace.TraceAutoConfiguration | top.continew.starter.web.autoconfigure.trace.TraceAutoConfiguration | ||||||
| top.continew.starter.web.autoconfigure.xss.XssAutoConfiguration | top.continew.starter.web.autoconfigure.xss.XssAutoConfiguration | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user