100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > SpringMVC 之类型转换Converter详解转载

SpringMVC 之类型转换Converter详解转载

时间:2021-06-24 11:26:39

相关推荐

SpringMVC 之类型转换Converter详解转载

SpringMVC之类型转换Converter详解

本文转载

/articles/uUjaum

1.1目录

1.1目录

1.2前言

1.3Converter接口

1.4ConversionService接口

1.5ConverterFactory接口

1.6GenericConverter接口

1.6.1概述

1.6.2ConditionalGenericConverter接口

1.2前言

在以往我们需要SpringMVC为我们自动进行类型转换的时候都是用的PropertyEditor。通过PropertyEditor的setAsText()方法我们可以实现字符串向特定类型的转换。但是这里有一个限制是它只支持从String类型转为其他类型。在Spring3中引入了一个Converter接口,它支持从一个Object转为另一个Object。除了Converter接口之外,实现ConverterFactory接口和GenericConverter接口也可以实现我们自己的类型转换逻辑。

1.3Converter接口

我们先来看一下Converter接口的定义:

Java代码 publicinterfaceConverter<S,T>{Tconvert(Ssource);}

我们可以看到这个接口是使用了泛型的,第一个类型表示原类型,第二个类型表示目标类型,然后里面定义了一个convert方法,将原类型对象作为参数传入进行转换之后返回目标类型对象。当我们需要建立自己的converter的时候就可以实现该接口。下面假设有这样一个需求,有一个文章实体,在文章中是可以有附件的,而附件我们需要记录它的请求地址、大小和文件名,所以这个时候文章应该是包含一个附件列表的。在实现的时候我们的附件是实时上传的,上传后由服务端返回对应的附件请求地址、大小和文件名,附件信息不直接存放在数据库中,而是作为文章的属性一起存放在Mongodb中。客户端获取到这些信息以后做一个简单的展示,然后把它们封装成特定格式的字符串作为隐藏域跟随文章一起提交到服务端。在服务端我们就需要把这些字符串附件信息转换为对应的List<Attachment>。所以这个时候我们就建立一个String[]到List<Attachment>的Converter。代码如下:

Java代码 importjava.util.ArrayList;importjava.util.List;importorg.springframework.core.convert.converter.Converter;importcom.tiantian.blog.model.Attachment;publicclassStringArrayToAttachmentListimplementsConverter<String[],List<Attachment>>{@OverridepublicList<Attachment>convert(String[]source){if(source==null)returnnull;List<Attachment>attachs=newArrayList<Attachment>(source.length);Attachmentattach=null;for(StringattachStr:source){//这里假设我们的Attachment是以“name,requestUrl,size”的形式拼接的。String[]attachInfos=attachStr.split(",");if(attachInfos.length!=3)//当按逗号分隔的数组长度不为3时就抛一个异常,说明非法操作了。thrownewRuntimeException();Stringname=attachInfos[0];StringrequestUrl=attachInfos[1];intsize;try{size=Integer.parseInt(attachInfos[2]);}catch(NumberFormatExceptione){thrownewRuntimeException();//这里也要抛一个异常。}attach=newAttachment(name,requestUrl,size);attachs.add(attach);}returnattachs;}}

1.4ConversionService接口

在定义好Converter之后,就是使用Converter了。为了统一调用Converter进行类型转换,Spring为我们提供了一个ConversionService接口。通过实现这个接口我们可以实现自己的Converter调用逻辑。我们先来看一下ConversionService接口的定义:

Java代码 publicinterfaceConversionService{booleancanConvert(Class<?>sourceType,Class<?>targetType);<T>Tconvert(Objectsource,Class<T>targetType);booleancanConvert(TypeDescriptorsourceType,TypeDescriptortargetType);Objectconvert(Objectsource,TypeDescriptorsourceType,TypeDescriptortargetType);}

我们可以看到ConversionService接口里面定义了两个canConvert方法和两个convert方法,canConvert方法用于判断当前的ConversionService是否能够对原类型和目标类型进行转换,convert方法则是用于进行类型转换的。上面出现的参数类型TypeDescriptor是对于一种类型的封装,里面包含该种类型的值、实际类型等等信息。

在定义了ConversionService之后我们就可以把它定义为一个bean对象,然后指定<mvn:annotation-driven/>的conversion-service属性为我们自己定义的ConversionService bean对象。如:

Xml代码 <mvc:annotation-drivenconversion-service="myConversionService"/><beanid="myConversionService"class="com.tiantian.blog.web.converter.support.MyConversionService"/>

这样当SpringMVC需要进行类型转换的时候就会调用ConversionService的canConvert和convert方法进行类型转换。

一般而言我们在实现ConversionService接口的时候也会实现ConverterRegistry接口。使用ConverterRegistry可以使我们对类型转换器做一个统一的注册。ConverterRegistry接口的定义如下:

Java代码 publicinterfaceConverterRegistry{voidaddConverter(Converter<?,?>converter);voidaddConverter(GenericConverterconverter);voidaddConverterFactory(ConverterFactory<?,?>converterFactory);voidremoveConvertible(Class<?>sourceType,Class<?>targetType);}

正如前言所说的,要实现自己的类型转换逻辑我们可以实现Converter接口、ConverterFactory接口和GenericConverter接口,ConverterRegistry接口就分别为这三种类型提供了对应的注册方法,至于里面的逻辑就可以发挥自己的设计能力进行设计实现了。

对于ConversionService,Spring已经为我们提供了一个实现,它就是GenericConversionService,位于org.springframework.core.convert.support包下面,它实现了ConversionService接口和ConverterRegistry接口。但是不能直接把它作为SpringMVC的ConversionService,因为直接使用时不能往里面注册类型转换器。也就是说不能像下面这样使用:

Xml代码 <mvc:annotation-drivenconversion-service="conversionService"/><beanid="conversionService"class="org.springframework.core.convert.support.GenericConversionService"/>

为此我们必须对GenericConversionService做一些封装,比如说我们可以在自己的ConversionService里面注入一个GenericConversionService,然后通过自己的ConversionService的属性接收Converter并把它们注入到GenericConversionService中,之后所有关于ConversionService的方法逻辑都可以调用GenericConversionService对应的逻辑。按照这种思想我们的ConversionService大概是这样的:

Java代码 packagecom.tiantian.blog.web.converter.support;importjava.util.Set;importjavax.annotation.PostConstruct;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.core.convert.ConversionService;importorg.springframework.core.convert.TypeDescriptor;importorg.springframework.core.convert.converter.Converter;importorg.springframework.core.convert.converter.ConverterFactory;importorg.springframework.core.convert.converter.GenericConverter;importorg.springframework.core.convert.support.GenericConversionService;publicclassMyConversionServiceimplementsConversionService{@AutowiredprivateGenericConversionServiceconversionService;privateSet<?>converters;@PostConstructpublicvoidafterPropertiesSet(){if(converters!=null){for(Objectconverter:converters){if(converterinstanceofConverter<?,?>){conversionService.addConverter((Converter<?,?>)converter);}elseif(converterinstanceofConverterFactory<?,?>){conversionService.addConverterFactory((ConverterFactory<?,?>)converter);}elseif(converterinstanceofGenericConverter){conversionService.addConverter((GenericConverter)converter);}}}}@OverridepublicbooleancanConvert(Class<?>sourceType,Class<?>targetType){returnconversionService.canConvert(sourceType,targetType);}@OverridepublicbooleancanConvert(TypeDescriptorsourceType,TypeDescriptortargetType){returnconversionService.canConvert(sourceType,targetType);}@Overridepublic<T>Tconvert(Objectsource,Class<T>targetType){returnconversionService.convert(source,targetType);}@OverridepublicObjectconvert(Objectsource,TypeDescriptorsourceType,TypeDescriptortargetType){returnconversionService.convert(source,sourceType,targetType);}publicSet<?>getConverters(){returnconverters;}publicvoidsetConverters(Set<?>converters){this.converters=converters;}}

在上面代码中,通过converters属性我们可以接收需要注册的Converter、ConverterFactory和GenericConverter,在converters属性设置完成之后afterPropertiesSet方法会被调用,在这个方法里面我们把接收到的converters都注册到注入的GenericConversionService中了,之后关于ConversionService的其他操作都是通过这个GenericConversionService来完成的。这个时候我们的SpringMVC文件可以这样配置:

Xml代码 <mvc:annotation-drivenconversion-service="conversionService"/><beanid="genericConversionService"class="org.springframework.core.convert.support.GenericConversionService"/><beanid="conversionService"class="com.tiantian.blog.web.converter.support.MyConversionService"><propertyname="converters"><set><beanclass="com.tiantian.blog.web.converter.StringArrayToAttachmentList"/></set></property></bean>

除了以上这种使用GenericConversionService的思想之外,Spring已经为我们提供了一个既可以使用GenericConversionService,又可以注入Converter的类,那就是ConversionServiceFactoryBean。该类为我们提供了一个可以接收Converter的converters属性,在它的内部有一个GenericConversionService对象的引用,在对象初始化完成之后它会new一个GenericConversionService对象,并往GenericConversionService中注册converters属性指定的Converter和Spring自身已经实现了的默认Converter,之后每次返回的都是这个GenericConversionService对象。当使用ConversionServiceFactoryBean的时候我们的SpringMVC文件可以这样配置:

Xml代码 <mvc:annotation-drivenconversion-service="conversionService"/><beanid="conversionService"class="org.springframework.context.support.ConversionServiceFactoryBean"><propertyname="converters"><list><beanclass="com.tiantian.blog.web.converter.StringArrayToAttachmentList"/></list></property></bean>

除了ConversionServiceFactoryBean之外,Spring还为我们提供了一个FormattingConversionServiceFactoryBean。当使用FormattingConversionServiceFactoryBean的时候我们的SpringMVC配置文件的定义应该是这样:

Xml代码 <mvc:annotation-drivenconversion-service="conversionService"/><beanid="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><propertyname="converters"><set><beanclass="com.tiantian.blog.web.converter.StringArrayToAttachmentList"/></set></property></bean>

以上介绍的是SpringMVC自动进行类型转换时需要我们做的操作。如果我们需要在程序里面手动的来进行类型转换的话,我们也可以往我们的程序里面注入一个ConversionService,然后通过ConversionService来进行相应的类型转换操作,也可以把Converter直接注入到我们的程序中。

1.5ConverterFactory接口

ConverterFactory的出现可以让我们统一管理一些相关联的Converter。顾名思义,ConverterFactory就是产生Converter的一个工厂,确实ConverterFactory就是用来产生Converter的。我们先来看一下ConverterFactory接口的定义:

Java代码 publicinterfaceConverterFactory<S,R>{<TextendsR>Converter<S,T>getConverter(Class<T>targetType);}

我们可以看到ConverterFactory接口里面就定义了一个产生Converter的getConverter方法,参数是目标类型的class。我们可以看到ConverterFactory中一共用到了三个泛型,S、R、T,其中S表示原类型,R表示目标类型,T是类型R的一个子类。

考虑这样一种情况,我们有一个表示用户状态的枚举类型UserStatus,如果要定义一个从String转为UserStatus的Converter,根据之前Converter接口的说明,我们的StringToUserStatus大概是这个样子:

Java代码 publicclassStringToUserStatusimplementsConverter<String,UserStatus>{@OverridepublicUserStatusconvert(Stringsource){if(source==null){returnnull;}returnUserStatus.valueOf(source);}}

如果这个时候有另外一个枚举类型UserType,那么我们就需要定义另外一个从String转为UserType的Converter——StringToUserType,那么我们的StringToUserType大概是这个样子:

Java代码 publicclassStringToUserTypeimplementsConverter<String,UserType>{@OverridepublicUserTypeconvert(Stringsource){if(source==null){returnnull;}returnUserType.valueOf(source);}}

如果还有其他枚举类型需要定义原类型为String的Converter的时候,我们还得像上面那样定义对应的Converter。有了ConverterFactory之后,这一切都变得非常简单,因为UserStatus、UserType等其他枚举类型同属于枚举,所以这个时候我们就可以统一定义一个从String到Enum的ConverterFactory,然后从中获取对应的Converter进行convert操作。Spring官方已经为我们实现了这么一个StringToEnumConverterFactory:

Java代码 @SuppressWarnings("unchecked")finalclassStringToEnumConverterFactoryimplementsConverterFactory<String,Enum>{public<TextendsEnum>Converter<String,T>getConverter(Class<T>targetType){returnnewStringToEnum(targetType);}privateclassStringToEnum<TextendsEnum>implementsConverter<String,T>{privatefinalClass<T>enumType;publicStringToEnum(Class<T>enumType){this.enumType=enumType;}publicTconvert(Stringsource){if(source.length()==0){//It'sanemptyenumidentifier:resettheenumvaluetonull.returnnull;}return(T)Enum.valueOf(this.enumType,source.trim());}}}

这样,如果是要进行String到UserStatus的转换,我们就可以通过StringToEnumConverterFactory实例的getConverter(UserStatus.class).convert(string)获取到对应的UserStatus,如果是要转换为UserType的话就是getConverter(UserType.class).convert(string)。这样就非常方便,可以很好的支持扩展。

对于ConverterFactory我们也可以把它当做ConvertionServiceFactoryBean的converters属性进行注册,在ConvertionServiceFactoryBean内部进行Converter注入的时候会根据converters属性具体元素的具体类型进行不同的注册,对于FormattingConversionServiceFactoryBean也是同样的方式进行注册。所以如果我们自己定义了一个StringToEnumConverterFactory,我们可以这样来进行注册:

Xml代码 <beanid="conversionService"class="org.springframework.context.support.ConversionServiceFactoryBean"><propertyname="converters"><list><beanclass="com.tiantian.blog.web.converter.StringArrayToAttachmentList"/><beanclass="com.tiantian.blog.web.converter.StringToEnumConverterFactory"/></list></property></bean>

1.6GenericConverter接口

1.6.1概述

GenericConverter接口是所有的Converter接口中最灵活也是最复杂的一个类型转换接口。像我们之前介绍的Converter接口只支持从一个原类型转换为一个目标类型;ConverterFactory接口只支持从一个原类型转换为一个目标类型对应的子类型;而GenericConverter接口支持在多个不同的原类型和目标类型之间进行转换,这也就是GenericConverter接口灵活和复杂的地方。

我们先来看一下GenericConverter接口的定义:

Java代码 publicinterfaceGenericConverter{Set<ConvertiblePair>getConvertibleTypes();Objectconvert(Objectsource,TypeDescriptorsourceType,TypeDescriptortargetType);publicstaticfinalclassConvertiblePair{privatefinalClass<?>sourceType;privatefinalClass<?>targetType;publicConvertiblePair(Class<?>sourceType,Class<?>targetType){Assert.notNull(sourceType,"Sourcetypemustnotbenull");Assert.notNull(targetType,"Targettypemustnotbenull");this.sourceType=sourceType;this.targetType=targetType;}publicClass<?>getSourceType(){returnthis.sourceType;}publicClass<?>getTargetType(){returnthis.targetType;}}}

我们可以看到GenericConverter接口中一共定义了两个方法,getConvertibleTypes()和convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)。getConvertibleTypes方法用于返回这个GenericConverter能够转换的原类型和目标类型的这么一个组合;convert方法则是用于进行类型转换的,我们可以在这个方法里面实现我们自己的转换逻辑。之所以说GenericConverter是最复杂的是因为它的转换方法convert的参数类型TypeDescriptor是比较复杂的。TypeDescriptor对类型Type进行了一些封装,包括value、Field及其对应的真实类型等等,具体的可以查看API。

关于GenericConverter的使用,这里也举一个例子。假设我们有一项需求是希望能通过user的id或者username直接转换为对应的user对象,那么我们就可以针对于id和username来建立一个GenericConverter。这里假设id是int型,而username是String型的,所以我们的GenericConverter可以这样来写:

Java代码 publicclassUserGenericConverterimplementsGenericConverter{@AutowiredprivateUserServiceuserService;@OverridepublicObjectconvert(Objectsource,TypeDescriptorsourceType,TypeDescriptortargetType){if(source==null||sourceType==TypeDescriptor.NULL||targetType==TypeDescriptor.NULL){returnnull;}Useruser=null;if(sourceType.getType()==Integer.class){user=userService.findById((Integer)source);//根据id来查找user}elseif(sourceType.getType()==String.class){user=userService.find((String)source);//根据用户名来查找user}returnuser;}@OverridepublicSet<ConvertiblePair>getConvertibleTypes(){Set<ConvertiblePair>pairs=newHashSet<ConvertiblePair>();pairs.add(newConvertiblePair(Integer.class,User.class));pairs.add(newConvertiblePair(String.class,User.class));returnpairs;}}

我们可以看到在上面定义的UserGenericConverter中,我们在getConvertibleTypes方法中添加了两组转换的组合,Integer到User和String到User。然后我们给UserGenericConverter注入了一个UserService,在convert方法

中我们简单的根据原类型是Integer还是String来判断传递的原数据是id还是username,并利用UserService对应的方法返回相应的User对象。

GenericConverter接口实现类的注册方法跟Converter接口和ConverterFactory接口实现类的注册方法是一样的,这里就不再赘述了。

虽然Converter接口、ConverterFactory接口和GenericConverter接口之间没有任何的关系,但是Spring内部在注册Converter实现类和ConverterFactory实现类时是先把它们转换为GenericConverter,之后再统一对GenericConverter进行注册的。也就是说Spring内部会把Converter和ConverterFactory全部转换为GenericConverter进行注册,在Spring注册的容器中只存在GenericConverter这一种类型转换器。我想之所以给用户开放Converter接口和ConverterFactory接口是为了让我们能够更方便的实现自己的类型转换器。基于此,Spring官方也提倡我们在进行一些简单类型转换器定义时更多的使用Converter接口和ConverterFactory接口,在非必要的情况下少使用GenericConverter接口。

1.6.2ConditionalGenericConverter接口

对于GenericConverter接口Spring还为我们提供了一个它的子接口,叫做ConditionalGenericConverter,在这个接口中只定义了一个方法:matches方法。我们一起来看一下ConditionalGenericConverter接口的定义:

Java代码 publicinterfaceConditionalGenericConverterextendsGenericConverter{booleanmatches(TypeDescriptorsourceType,TypeDescriptortargetType);}

顾名思义,从Conditional我们就可以看出来这个接口是用于定义有条件的类型转换器的,也就是说不是简单的满足类型匹配就可以使用该类型转换器进行类型转换了,必须要满足某种条件才能使用该类型转换器。而该类型转换器的条件控制就是通过ConditionalGenericConverter接口的matches方法来实现的。关于ConditionalGenericConverter的使用Spring内部已经实现了很多,这里我们来看一个Spring已经实现了的将String以逗号分割转换为目标类型数组的实现:

Java代码 finalclassStringToArrayConverterimplementsConditionalGenericConverter{privatefinalConversionServiceconversionService;publicStringToArrayConverter(ConversionServiceconversionService){this.conversionService=conversionService;}publicSet<ConvertiblePair>getConvertibleTypes(){returnCollections.singleton(newConvertiblePair(String.class,Object[].class));}publicbooleanmatches(TypeDescriptorsourceType,TypeDescriptortargetType){returnthis.conversionService.canConvert(sourceType,targetType.getElementTypeDescriptor());}publicObjectconvert(Objectsource,TypeDescriptorsourceType,TypeDescriptortargetType){if(source==null){returnnull;}Stringstring=(String)source;String[]fields=maDelimitedListToStringArray(string);Objecttarget=Array.newInstance(targetType.getElementType(),fields.length);for(inti=0;i<fields.length;i++){ObjectsourceElement=fields[i];ObjecttargetElement=this.conversionService.convert(sourceElement,sourceType,targetType.getElementTypeDescriptor());Array.set(target,i,targetElement);}returntarget;}}

我们可以看到这个StringToArrayConverter就是实现了ConditionalGenericConverter接口的。根据里面的matches方法的逻辑我们知道当我们要把一个字符串转换为一个数组的时候,只有我们已经定义了一个字符串到这个目标数组元素对应类型的类型转换器时才可以使用StringToArrayConverter进行类型转换。也就是说假如我们已经定义了一个String到User的类型转换器,那么当我们需要将String转换为对应的User数组的时候,我们就可以直接使用Spring为我们提供的StringToArrayConverter了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。