在mvc中注册各种类型转换器(日期类型 json类型转换)
2017-12-31 15:51
363 查看
1. databind
Servlet中的输入参数为都是string类型,而spring mvc通过data bind机制将这些string 类型的输入参数转换为相应的command object(根据view和controller之间传输数据的具体逻辑,也可称为model attributes, domain model objects)。在这个转换过程中,spring实际是先利用java.beans.PropertyEditor中的 setAdText方法来把string格式的输入转换为bean属性, 亦可通过继承java.beans.PropertyEditorSupport来实现自定义的PropertyEditors,具体实现方式可参考spring
reference 3.0.5 第 5.4节中的 Registering additional custom PropertyEditors部分。
方式1:
[html] view
plain copy
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.management.utils.propeditor.BindingInitializer" />
</property>
</bean>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean" >
</bean>
方式2:
[html] view
plain copy
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="net.zhepu.web.customerBinding.CustomerConverter" />
</list>
</property>
</bean>
<mvc:annotation-driven validator="validator"
conversion-service="conversionService" />
注意: 方式一通过 AnnotationMethodHandlerAdapter注册webBindingInitializer来添加自定义类型转换, 又通过ConversionServiceFactoryBean注册默认的类型转换。
方式二通过 mvc:annotation-driven来添加自定义转换, mvc:annotation-driven 是spring 3后出现的新的标注, 他不能和AnnotationMethodHandlerAdapter同时使用, 否则会出现冲突。
2. HttpMessageConverter
Spring MVC中对于requestBody中发送的数据转换不是通过databind来实现,而是使用HttpMessageConverter来实现具体的类型转换。 例如,之前提到的json格式的输入,在将json格式的输入转换为具体的model的过程中,spring mvc首先找出request header中的contenttype,再遍历当前所注册的所有的HttpMessageConverter子类, 根据子类中的canRead()方法来决定调用哪个具体的子类来实现对requestBody中的数据的解析。如果当前所注册的httpMessageConverter中都无法解析对应contexttype类型,则抛出HttpMediaTypeNotSupportedException
(http 415错误)。
那么需要如何注册自定义的messageConverter呢,很不幸,在spring 3.0.5中如果使用annotation-driven的配置方式的话,无法实现自定义的messageConverter的配置,必须老老实实的自己定义AnnotationMethodHandlerAdapter的bean定义,再设置其messageConverters以注册自定义的messageConverter。
方式1:
[html] view
plain copy
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.management.utils.propeditor.BindingInitializer" />
</property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
方式2:
在3.1版本中,将增加annotation-driven对自定义的messageConverter的支持 (SPR-7504),具体格式如下 :
同样, 上面方式1和方式2的不能同时使用, 只能同时选用一种方式
Servlet中的输入参数为都是string类型,而spring mvc通过data bind机制将这些string 类型的输入参数转换为相应的command object(根据view和controller之间传输数据的具体逻辑,也可称为model attributes, domain model objects)。在这个转换过程中,spring实际是先利用java.beans.PropertyEditor中的 setAdText方法来把string格式的输入转换为bean属性, 亦可通过继承java.beans.PropertyEditorSupport来实现自定义的PropertyEditors,具体实现方式可参考spring
reference 3.0.5 第 5.4节中的 Registering additional custom PropertyEditors部分。
方式1:
[html] view
plain copy
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.management.utils.propeditor.BindingInitializer" />
</property>
</bean>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean" >
</bean>
方式2:
[html] view
plain copy
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="net.zhepu.web.customerBinding.CustomerConverter" />
</list>
</property>
</bean>
<mvc:annotation-driven validator="validator"
conversion-service="conversionService" />
注意: 方式一通过 AnnotationMethodHandlerAdapter注册webBindingInitializer来添加自定义类型转换, 又通过ConversionServiceFactoryBean注册默认的类型转换。
方式二通过 mvc:annotation-driven来添加自定义转换, mvc:annotation-driven 是spring 3后出现的新的标注, 他不能和AnnotationMethodHandlerAdapter同时使用, 否则会出现冲突。
2. HttpMessageConverter
Spring MVC中对于requestBody中发送的数据转换不是通过databind来实现,而是使用HttpMessageConverter来实现具体的类型转换。 例如,之前提到的json格式的输入,在将json格式的输入转换为具体的model的过程中,spring mvc首先找出request header中的contenttype,再遍历当前所注册的所有的HttpMessageConverter子类, 根据子类中的canRead()方法来决定调用哪个具体的子类来实现对requestBody中的数据的解析。如果当前所注册的httpMessageConverter中都无法解析对应contexttype类型,则抛出HttpMediaTypeNotSupportedException
(http 415错误)。
那么需要如何注册自定义的messageConverter呢,很不幸,在spring 3.0.5中如果使用annotation-driven的配置方式的话,无法实现自定义的messageConverter的配置,必须老老实实的自己定义AnnotationMethodHandlerAdapter的bean定义,再设置其messageConverters以注册自定义的messageConverter。
方式1:
[html] view
plain copy
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.management.utils.propeditor.BindingInitializer" />
</property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
方式2:
在3.1版本中,将增加annotation-driven对自定义的messageConverter的支持 (SPR-7504),具体格式如下 :
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>
同样, 上面方式1和方式2的不能同时使用, 只能同时选用一种方式
相关文章推荐
- Json 的日期格式与.Net DateTime类型的转换
- [日期转换]将各种类型的日期格式化
- springMVC全局日期转换(@RequestBody json类型的)
- java中各种日期类型转换
- springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
- Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常
- Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型
- springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
- json日期类型转换
- 通过自己定义MVC的Controller的Json转换器解决日期序列化格式问题
- SpringMVC @RequestBody接收json字符串,自动将日期字符串转换为java.util.Date
- Json 的日期格式与.Net DateTime类型的转换
- android开发过程中将json日期字符串,转换成JAVA中Date类型格式化字符串
- 将Date日期类型的字段转换成json字符串
- 各种日期类型之间的转换
- Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常
- java与json互相转换(解决日期问题),js显示json数据时出现日期类型显示[object,object]现象解决!
- Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型
- Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型
- initBinder解决Json日期转换为date类型