SpringMVC配置全局日期轉換器,處理日期轉換異常


Spring 3.1.1使用Mvc配置全局日期轉換器,處理日期轉換異常鏈接地址:

https://www.2cto.com/kf/201308/236837.html

spring3.0配置日期轉換可以通過配置自定義實現WebBingingInitializer接口的一個日期轉換類來實現,方法如下

轉換類:
public class DateConverter implements WebBindingInitializer {    
    
    public void initBinder(WebDataBinder binder, WebRequest request) {    
          
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");    
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));    
    }    
} 

在spring-servlet.xml當中的進行注冊:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
    <!-- 日期格式轉換 -->    
    <property name="webBindingInitializer">    
        <bean class="DateConverter" />    
    </property>    
</bean>  
 

spring3.1.1及更高版本的配置方法

spring3.1.1的處理進行調整,所以按照3.0的寫法在3.1.1里面是無效的,通過查找資料及測試,發現可行方法

 

原因:

 

annotation-driven缺省注冊類的改變 

Spring 3.0.x中使用了annotation-driven后,缺省使用DefaultAnnotationHandlerMapping 來注冊handler method和request的mapping關系。 AnnotationMethodHandlerAdapter來在實際調用handlermethod前對其參數進行處理。 

在spring mvc 3.1中,對應變更為 
DefaultAnnotationHandlerMapping -> RequestMappingHandlerMapping 
AnnotationMethodHandlerAdapter -> RequestMappingHandlerAdapter 
AnnotationMethodHandlerExceptionResolver -> ExceptionHandlerExceptionResolver 

解決方法:

使用conversion-service來注冊自定義的converter 
DataBinder實現了PropertyEditorRegistry, TypeConverter這兩個interface,而在spring mvc實際處理時,返回值都是return binder.convertIfNecessary(見HandlerMethodInvoker中的具體處理邏輯)。因此可以使用customer conversionService來實現自定義的類型轉換。 

從<mvc:annotation-driven />中配置可以看出,AnnotationMethodHandlerAdapter已經配置了webBindingInitializer,我們可以通過設置其屬性conversionService來實現自定義類型轉換。

配置文件加入如下配置:

 

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">    
        <property name="converters">    
            <list>    
                <bean class="com.doje.XXX.web.DateConverter" />    
            </list>    
        </property>    
    </bean>  

 

 

 

需要修改spring service context xml配置文件中的annotation-driven,增加屬性conversion-service指向新增的conversionService bean。 

 

<mvc:annotation-driven conversion-service="conversionService" />   
 

 

實際自定義的converter如下。 

Java代碼  
public class DateConverter implements Converter<String, Date> {    
@Override    
public Date convert(String source) {    
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
    dateFormat.setLenient(false);    
    try {    
        return dateFormat.parse(source);    
    } catch (ParseException e) {    
        e.printStackTrace();    
    }           
    return null;    
}    
 

第三種配置方法

 

SpringMVC文件配制:

<mvc:annotation-driven>  
    <!-- 處理responseBody 里面日期類型 -->  
        <mvc:message-converters>  
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                <property name="objectMapper">  
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
                        <property name="dateFormat">  
                            <bean class="java.text.SimpleDateFormat">  
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                            </bean>  
                        </property>  
                    </bean>  
                </property>  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>

特殊日期格式配制使用(@JsonFormat)在get方法上,如下:

@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
 public Date getBirth() {
      return birth;
 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM