您的位置:首页 > 健康 > 养生 > 下载手机app客户端下载安装_浙江省建设通网站_seo诊断工具_百度指数关键词未收录怎么办

下载手机app客户端下载安装_浙江省建设通网站_seo诊断工具_百度指数关键词未收录怎么办

2025/5/10 12:06:53 来源:https://blog.csdn.net/zp357252539/article/details/147061221  浏览:    关键词:下载手机app客户端下载安装_浙江省建设通网站_seo诊断工具_百度指数关键词未收录怎么办
下载手机app客户端下载安装_浙江省建设通网站_seo诊断工具_百度指数关键词未收录怎么办

以下是Spring Boot默认注册的转换器列表及其功能说明。这些转换器使得控制器方法可以直接接收IntegerLongDate等类型参数,无需手动实现转换:


默认转换器列表及功能

1. 基础类型转换器
转换器名称功能示例场景
StringToIntegerConverter将字符串转换为IntegerURL参数?count=123Integer count
StringToLongConverter将字符串转换为LongURL参数?id=1000LLong id
StringToDoubleConverter将字符串转换为DoubleURL参数?price=19.99Double price
StringToBooleanConverter将字符串(如true/false)转换为BooleanURL参数?active=trueBoolean active

2. 日期时间转换器
转换器名称功能示例场景
StringToDateConverter将字符串转换为java.util.Date(默认格式yyyy-MM-dd HH:mm:ssURL参数?date=2023-10-01Date date
StringToLocalDateConverter将字符串转换为java.time.LocalDate(默认格式yyyy-MM-ddURL参数?birthday=1990-01-01LocalDate birthday
StringToLocalDateTimeConverter转换为LocalDateTime(默认格式yyyy-MM-dd HH:mm:ssURL参数?createTime=2023-10-01T12:30:00LocalDateTime createTime

3. 集合与数组转换器
转换器名称功能示例场景
StringToArrayConverter将逗号分隔的字符串转换为数组(如String[]URL参数?tags=a,b,cString[] tags
StringToCollectionConverter转换为集合类型(如List<String>Set<String>URL参数?ids=1,2,3List<String> ids

4. 枚举与特殊类型转换器
转换器名称功能示例场景
StringToEnumConverter将字符串转换为枚举类型(通过枚举名称匹配)URL参数?status=ACTIVEStatusEnum status
StringToUUIDConverter将字符串转换为UUIDURL参数?uuid=550e8400-e29b-41d4-a716-446655440000UUID uuid
StringToDurationConverter将字符串(如"10s")转换为java.time.DurationURL参数?timeout=30sDuration timeout

5. 其他实用转换器
转换器名称功能示例场景
SpelExpressionConverter使用SpEL表达式解析参数(需开启spring.expression.spel-enabled=trueURL参数?size=#{T(java.lang.Math).random()} → 动态计算值
StringToURIConverter将字符串转换为URI对象URL参数?url=http://example.comURI url

默认转换器的注册机制

Spring Boot在启动时通过以下方式自动注册这些转换器:

  1. FormattingConversionServiceFactoryBean

    • 注册基础类型、日期、枚举等转换器。
    • 支持@NumberFormat@DateTimeFormat注解。
  2. WebConversionService

    • 扩展FormattingConversionService,添加Web相关的转换器(如StringToArrayConverter)。

如何查看所有已注册的转换器?

可以通过以下代码在控制器中输出所有转换器:

import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;// 示例:在配置类中打印所有转换器
@Configuration
public class ConversionServiceConfig implements WebMvcConfigurer {@Autowiredprivate RequestMappingHandlerAdapter adapter;@PostConstructpublic void printConverters() {ConfigurableWebBindingInitializer bindingInitializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();DefaultConversionService conversionService = (DefaultConversionService) bindingInitializer.getConversionService();conversionService.getConverters().forEach(converter -> System.out.println("Registered Converter: " + converter.getClass().getName()));}
}

使用默认转换器的注意事项

  1. 格式问题

    • 若日期格式不符合默认规则(如需dd/MM/yyyy),需通过@DateTimeFormat或自定义转换器修改。
    • 数字类型(如Double)对非数字字符串(如"abc")会抛出异常。
  2. 类型冲突

    • 若多个转换器匹配同一类型,优先级由Spring决定(如Converter优先于GenericConverter)。
  3. 自定义扩展

    • 可通过实现ConverterFormatter接口,并注册到WebMvcConfigurer中覆盖默认行为。

总结表格

类型支持的转换默认格式/规则
基础类型String → Integer/Long/Double/Boolean直接解析字符串为对应类型
日期时间String → Date/LocalDate/LocalDateTimeyyyy-MM-dd HH:mm:ss(可自定义)
集合/数组String → String[]/List/Set逗号分隔符(如a,b,c
枚举String → Enum枚举名称匹配(不区分大小写)
特殊类型String → UUID/Duration/URI标准格式(如UUID的xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

通过这些默认转换器,Spring Boot大幅简化了参数处理,但复杂场景仍需自定义扩展。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com