当前位置: 首页 > news >正文

wordpress原创公司主题淄博seo推广

wordpress原创公司主题,淄博seo推广,上海 建设工程质量监督站网站,hk域名哪里注册在Spring中经常需要各种数据类型之间进行转换,比如配置文件中的数据转换为代码所需要的数据类型,在使用SpringMvc的时候,将前台传来的参数自动转换为我们接收参数时定义的类型。 Spring中的ConversionService就是提供这种服务的 1.DefaultC…
在Spring中经常需要各种数据类型之间进行转换,比如配置文件中的数据转换为代码所需要的数据类型,在使用SpringMvc的时候,将前台传来的参数自动转换为我们接收参数时定义的类型。

Spring中的ConversionService就是提供这种服务的

1.DefaultConversionService

DefaultConversionService是一种默认的实现,如果我们不定义,就是使用默认的
@Test
public void defaultConversionService(){ConversionService conversionService = new DefaultConversionService();//这里是校验这个DefaultConversionService是否支持将String转换为Integerboolean b = conversionService.canConvert(String.class, Integer.class);System.out.println(b);Long convert = conversionService.convert("123", Long.class);System.out.println(convert);String convert1 = conversionService.convert(new Date() , String.class);System.out.println(convert1);Date convert2 = conversionService.convert("Mon Nov 04 23:32:14 CST 2024" , Date.class);System.out.println(convert2);}运行结果:
true
123
Tue Nov 05 11:02:17 CST 2024
Tue Nov 05 13:32:14 CST 2024

我们可以看到这里DefaultConversionService 可以成功的将 String转换为Long,将Date转换为String,将String转换为Date

为什么DefaultConversionService能支持这么多数据类型之间的相互转换呢,我们看看他的源码就知道了,原来在DefaultConversionService构造方法中就调用addDefaultConverters()方法注册了我们常用的数据类型转换器Converter,如果我们需要转换的时候,它就会找一个能够完成转换的Converter进行转换

public class DefaultConversionService extends GenericConversionService {@Nullableprivate static volatile DefaultConversionService sharedInstance;/*** Create a new {@code DefaultConversionService} with the set of* {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}.*/public DefaultConversionService() {addDefaultConverters(this);}/*** Return a shared default {@code ConversionService} instance,* lazily building it once needed.* <p><b>NOTE:</b> We highly recommend constructing individual* {@code ConversionService} instances for customization purposes.* This accessor is only meant as a fallback for code paths which* need simple type coercion but cannot access a longer-lived* {@code ConversionService} instance any other way.* @return the shared {@code ConversionService} instance (never {@code null})* @since 4.3.5*/public static ConversionService getSharedInstance() {DefaultConversionService cs = sharedInstance;if (cs == null) {synchronized (DefaultConversionService.class) {cs = sharedInstance;if (cs == null) {cs = new DefaultConversionService();sharedInstance = cs;}}}return cs;}/*** Add converters appropriate for most environments.* @param converterRegistry the registry of converters to add to* (must also be castable to ConversionService, e.g. being a {@link ConfigurableConversionService})* @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService*/public static void addDefaultConverters(ConverterRegistry converterRegistry) {addScalarConverters(converterRegistry);addCollectionConverters(converterRegistry);converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));converterRegistry.addConverter(new StringToTimeZoneConverter());converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());converterRegistry.addConverter(new ObjectToObjectConverter());converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));converterRegistry.addConverter(new FallbackObjectToStringConverter());converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry));}/*** Add common collection converters.* @param converterRegistry the registry of converters to add to* (must also be castable to ConversionService, e.g. being a {@link ConfigurableConversionService})* @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService* @since 4.2.3*/public static void addCollectionConverters(ConverterRegistry converterRegistry) {ConversionService conversionService = (ConversionService) converterRegistry;converterRegistry.addConverter(new ArrayToCollectionConverter(conversionService));converterRegistry.addConverter(new CollectionToArrayConverter(conversionService));converterRegistry.addConverter(new ArrayToArrayConverter(conversionService));converterRegistry.addConverter(new CollectionToCollectionConverter(conversionService));converterRegistry.addConverter(new MapToMapConverter(conversionService));converterRegistry.addConverter(new ArrayToStringConverter(conversionService));converterRegistry.addConverter(new StringToArrayConverter(conversionService));converterRegistry.addConverter(new ArrayToObjectConverter(conversionService));converterRegistry.addConverter(new ObjectToArrayConverter(conversionService));converterRegistry.addConverter(new CollectionToStringConverter(conversionService));converterRegistry.addConverter(new StringToCollectionConverter(conversionService));converterRegistry.addConverter(new CollectionToObjectConverter(conversionService));converterRegistry.addConverter(new ObjectToCollectionConverter(conversionService));converterRegistry.addConverter(new StreamConverter(conversionService));}private static void addScalarConverters(ConverterRegistry converterRegistry) {converterRegistry.addConverterFactory(new NumberToNumberConverterFactory());converterRegistry.addConverterFactory(new StringToNumberConverterFactory());converterRegistry.addConverter(Number.class, String.class, new ObjectToStringConverter());converterRegistry.addConverter(new StringToCharacterConverter());converterRegistry.addConverter(Character.class, String.class, new ObjectToStringConverter());converterRegistry.addConverter(new NumberToCharacterConverter());converterRegistry.addConverterFactory(new CharacterToNumberFactory());converterRegistry.addConverter(new StringToBooleanConverter());converterRegistry.addConverter(Boolean.class, String.class, new ObjectToStringConverter());converterRegistry.addConverterFactory(new StringToEnumConverterFactory());converterRegistry.addConverter(new EnumToStringConverter((ConversionService) converterRegistry));converterRegistry.addConverterFactory(new IntegerToEnumConverterFactory());converterRegistry.addConverter(new EnumToIntegerConverter((ConversionService) converterRegistry));converterRegistry.addConverter(new StringToLocaleConverter());converterRegistry.addConverter(Locale.class, String.class, new ObjectToStringConverter());converterRegistry.addConverter(new StringToCharsetConverter());converterRegistry.addConverter(Charset.class, String.class, new ObjectToStringConverter());converterRegistry.addConverter(new StringToCurrencyConverter());converterRegistry.addConverter(Currency.class, String.class, new ObjectToStringConverter());converterRegistry.addConverter(new StringToPropertiesConverter());converterRegistry.addConverter(new PropertiesToStringConverter());converterRegistry.addConverter(new StringToUUIDConverter());converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter());}}

2.DefaultFormattingConversionService

有的时候我们需要将某一种数据类型转换为指定的字符串格式,也需要将指定格式的字符串转换为某一种数据类型,比如我们需要将Date转为为yyyy-MM-dd格式的字符串。我们在配置文件配置yyyy-MM-dd 格式的字符串,需要将其转换为Date类型的数据。在SpringMvc将数据返回给前端之前我们可能需要将Person类型的数据转换为JSON格式的字符串

这个时候我们就需要DefaultFormattingConversionService,它在完成数据类型转换的同时还支持将某种数据类型转换为指定格式的字符串,而且我们还可以自定义格式转换器来支持我们自定义的数据类型。

2.1 自定义Date和String之间转换的Formatter
@Test
public void defaultFormattingConversionService(){DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();conversionService.addFormatter(new Formatter<Date>() {@Overridepublic String print(Date object, Locale locale) {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object);}@Overridepublic Date parse(String text, Locale locale) throws ParseException {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(text);}});String convert1 = conversionService.convert(new Date() , String.class);System.out.println(convert1);Date convert2 = conversionService.convert("2024-11-04 23:52:36" , Date.class);System.out.println(convert2);}运行结果:
2024-11-05 11:48:33
Mon Nov 04 23:52:36 CST 2024

上面的代码我们自定义了一个Date和String的Formatter,那么DefaultFormattingConversionService在进行Date和String之间的相互转换时,就会用我们自定义的Formatter 进行格式化和解析

@Test
public void defaultFormattingConversionService(){DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();conversionService.addFormatter(new Formatter<Date>() {@Overridepublic String print(Date object, Locale locale) {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object);}@Overridepublic Date parse(String text, Locale locale) throws ParseException {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(text);}});String convert1 = conversionService.convert(new Date() , String.class);System.out.println(convert1);Date convert2 = conversionService.convert("Mon Nov 04 23:32:14 CST 2024" , Date.class);System.out.println(convert2);}运行结果:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value 'Mon Nov 04 23:32:14 CST 2024'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [Mon Nov 04 23:32:14 CST 2024]

这里报错了,说明了我们添加了自定义的Formatter之后,不支持将Mon Nov 04 23:32:14 CST 2024格式的字符串转换为Date类型了,支持支将yyyy-MM-dd HH:mm:ss格式的字符串转换为Date类型,说明之前默认的支持Mon Nov 04 23:32:14 CST 2024格式的Formatter被我们自定义Formatter给覆盖了

2.2 自定义Person和String之间转换的Formatter
@Test
public void defaultFormattingConversionService1(){DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();conversionService.addFormatter(new Formatter<Person>() {@Overridepublic String print(Person object, Locale locale) {return JSON.toJSONString(object);}@Overridepublic Person parse(String text, Locale locale) throws ParseException {return JSON.parseObject(text , Person.class);}});Person person = new Person();person.setName("孙悟空");person.setAge(20);String convert1 = conversionService.convert(person , String.class);System.out.println(convert1);Person convert2 = conversionService.convert(convert1 , Person.class);System.out.println(convert2);}运行结果:
{"age":20,"name":"孙悟空"}
Person{name='孙悟空', age=20}

我们看到成功将Person对象转换为我们自定义的JSON格式,也可以成功将JSON格式的字符串转换为Person对象


文章转载自:
http://caribbean.pzdurr.cn
http://cartographer.pzdurr.cn
http://anthropophagite.pzdurr.cn
http://athenai.pzdurr.cn
http://blepharitis.pzdurr.cn
http://anatropous.pzdurr.cn
http://aptotic.pzdurr.cn
http://bisexed.pzdurr.cn
http://betaine.pzdurr.cn
http://aphorism.pzdurr.cn
http://animalcule.pzdurr.cn
http://braky.pzdurr.cn
http://cannabic.pzdurr.cn
http://beaverette.pzdurr.cn
http://bemoisten.pzdurr.cn
http://amphibia.pzdurr.cn
http://blithesome.pzdurr.cn
http://caducity.pzdurr.cn
http://brownness.pzdurr.cn
http://artifical.pzdurr.cn
http://anemometer.pzdurr.cn
http://blowgun.pzdurr.cn
http://battery.pzdurr.cn
http://brushwood.pzdurr.cn
http://amphictyony.pzdurr.cn
http://antianxity.pzdurr.cn
http://argental.pzdurr.cn
http://capetown.pzdurr.cn
http://anticorrosion.pzdurr.cn
http://bowlful.pzdurr.cn
http://acmesthesia.pzdurr.cn
http://callback.pzdurr.cn
http://axisymmetric.pzdurr.cn
http://abyssalpelagic.pzdurr.cn
http://bolingbroke.pzdurr.cn
http://asphaltic.pzdurr.cn
http://bialy.pzdurr.cn
http://cherrapunji.pzdurr.cn
http://cathodal.pzdurr.cn
http://argentic.pzdurr.cn
http://chloramphenicol.pzdurr.cn
http://abuttal.pzdurr.cn
http://chalcenteric.pzdurr.cn
http://bordetela.pzdurr.cn
http://appellatively.pzdurr.cn
http://basilica.pzdurr.cn
http://basra.pzdurr.cn
http://buitenzorg.pzdurr.cn
http://belle.pzdurr.cn
http://abrogation.pzdurr.cn
http://charleston.pzdurr.cn
http://balloonkite.pzdurr.cn
http://butazolidin.pzdurr.cn
http://caseinate.pzdurr.cn
http://ayrshire.pzdurr.cn
http://awedness.pzdurr.cn
http://boong.pzdurr.cn
http://bibliophil.pzdurr.cn
http://alabamian.pzdurr.cn
http://battleship.pzdurr.cn
http://begat.pzdurr.cn
http://cateress.pzdurr.cn
http://arcanum.pzdurr.cn
http://caseous.pzdurr.cn
http://butyrate.pzdurr.cn
http://armoire.pzdurr.cn
http://anabasis.pzdurr.cn
http://cesser.pzdurr.cn
http://ceremony.pzdurr.cn
http://boned.pzdurr.cn
http://cello.pzdurr.cn
http://caip.pzdurr.cn
http://abirritate.pzdurr.cn
http://acquisitive.pzdurr.cn
http://anaclisis.pzdurr.cn
http://chairside.pzdurr.cn
http://botryoidal.pzdurr.cn
http://business.pzdurr.cn
http://bookcraft.pzdurr.cn
http://achy.pzdurr.cn
http://acapnia.pzdurr.cn
http://additament.pzdurr.cn
http://biblist.pzdurr.cn
http://capias.pzdurr.cn
http://certifiable.pzdurr.cn
http://bullace.pzdurr.cn
http://brewis.pzdurr.cn
http://calciphylaxis.pzdurr.cn
http://ably.pzdurr.cn
http://ameslan.pzdurr.cn
http://bimbo.pzdurr.cn
http://bernie.pzdurr.cn
http://chinar.pzdurr.cn
http://abbatial.pzdurr.cn
http://antihero.pzdurr.cn
http://bydgoszcz.pzdurr.cn
http://bioresearch.pzdurr.cn
http://abecedarian.pzdurr.cn
http://cacophonist.pzdurr.cn
http://callable.pzdurr.cn
http://www.tj-hxxt.cn/news/31134.html

相关文章:

  • 日照seo顾问搜索引擎优化的名词解释
  • 淄博网站建设优化seo百度站长资源
  • 网站建设功能要求网站推广是做什么的
  • 建网站费用明细百度seo工作室
  • 公司建立网站青岛电话发广告去哪个平台
  • 做企业网站收费多少钱seo公司优化排名
  • 可以做皮肤测试的网站百度站长平台官网登录入口
  • 上海php网站开发公司抖音引流推广怎么做
  • 手机黄山网站为什么打开网址都是站长工具
  • 上海跨境电商网站制作可以发外链的网站整理
  • 做网站需要的合同自动外链网址
  • 网站日常维护流程网络服务有限公司
  • 信誉好的镇江网站建设如何创建微信小程序
  • 网页设计一个月工资多少厦门seo网站推广
  • 电白手机网站建设公司百度学术论文查重官网
  • wordpress外贸主题seo网络营销推广公司
  • 客户关系管理案例经典西安seo服务商
  • 做竞争小的网站浙江网站建设平台
  • 用美国服务器做钓鱼网站连云港百度推广总代理
  • 网站开发成本会计分录站长工具a级
  • 武汉做搜索引擎推广的公司福州seo优化排名推广
  • 网络科技加我qq是干嘛深圳seo优化推广公司
  • 建立网站怎么做关键字公司推广渠道有哪些
  • 旅游网站建设价格企业网络营销方案
  • 网站空间是怎么开通的百度视频免费高清影视
  • html5的篮球网站开发如何在百度发广告
  • 网站建设熊猫建站百度指数官网数据
  • 家居网站建设公司排名企业网站建设的步骤
  • wordpress对seo汕头seo建站
  • 上海歌舞娱乐场所停业怎么优化网站