zoukankan      html  css  js  c++  java
  • Spring Mvc 传递参数要controller出现了400,日期参数全局处理,格式化yyyy-MM-dd 和yyyy-MM-dd HH:mm:ss

    描述:今天做一个业务操作的时候,ajax传递参数要controller出现了400,前后台都没有报错。

    问题:springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误

    解决:使用SpringMvc进行全局日期处理

    案例如下:

    1.Controller

    /** 
         * 接收日期类型参数 
         *     注意: 
         *         springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误 
         *  解决办法 
         *      1.全局日期处理 
         *  
         */  
          
        @RequestMapping("/test")  
        public String test(Date birthday){  
            System.out.println(birthday);  
            return "index";  
        } 
    

      

    2.自定义类型转换规则 

    SpringMvc提供了Converter接口,它支持从一个Object转换为另一个Object

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.core.convert.converter.Converter;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.support.WebBindingInitializer;
    import org.springframework.web.context.request.WebRequest;
    
    /**
     * 全局日期处理类
     * Convert<T,S>
     *         泛型T:代表客户端提交的参数 stringDate
     *         泛型S:通过convert转换的类型
    通过convert转换的类型
     */
    public class DateConvert implements Converter<String, Date> {
    
        @Override
        public Date convert(String stringDate) {
            SimpleDateFormat simpleDateFormat =null;
            if(stringDate.length()==10) {
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }
            else  if(stringDate.length()>10)
            {
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            try {
                return simpleDateFormat.parse(stringDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    }
    

    3.注册自定义的类型转换类

    <mvc:annotation-driven conversion-service="conversionService">
    		<!-- 改写@ResponseBody的返回值, 此处禁用Jackson序列化空对象报异常的特性 -->
    
    		<mvc:message-converters>
    			<bean
    				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    				<property name="objectMapper">
    					<bean class="com.cn.xf.common.ArcJacksonObjectMapper">
    					</bean>
    				</property>
    				<property name="supportedMediaTypes">
    					<list>
    						<value>text/plain;charset=UTF-8</value>
    						<value>application/json;charset=UTF-8</value>
    					</list>
    				</property>
    			</bean>		
    		</mvc:message-converters>
    		
    	</mvc:annotation-driven>
    
    
    	<!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
    	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    		<property name="converters">
    			<set>
    				<ref bean="dateConvert"/>
    			</set>
    		</property>
    	</bean>
    	<!-- 第一步:  创建自定义日期转换规则 -->
    	<bean id="dateConvert" class="com.cn.xf.common.DateConvert"/>
    

      

     
  • 相关阅读:
    Cronolog切割tomcat日志
    使用TortoiseGit+码云管理项目代码
    POI使用 (4.0) 常用改动
    MySQL-with rollup函数运用
    CentOS7使用yum安装MySQL8.0
    java 8 新特性之Stream的排序/分类
    java阳历转农历
    MyBatis SQL xml处理小于号与大于号
    mysql update不能直接使用select的结果
    Java面试中遇到的坑【填坑篇】
  • 原文地址:https://www.cnblogs.com/anakin/p/7670942.html
Copyright © 2011-2022 走看看