SpringMVC接收日期类型参数的自定义转换

一般类型的参数,都会自动实现转换,比如下面的 private Long id; private String userName; private BigDecimal userSalary; 但是时间类型没有对应转换规则的话,服务器就会报错。

public class User {
    private Long id;
    private String userName;
    private BigDecimal userSalary;
    private Date createTime;
  // getter、setter
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

实现spring提供的Converter接口,自定义类型转换的处理。

Converter

import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by susq on 2017-7-10.
 */
public class DateConvert implements Converter<String, Date> {

    @Override
    public Date convert(String stringDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date d = null;
        try {
            d = simpleDateFormat.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在spring配置文件中注册自定义的转换bean

    <bean id="dateConvert" class="com.susq.springbegin.utils.DateConvert"/>
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService"/>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Controller中如下。

    @RequestMapping(value = "/api/users/testParam")
    public void testparam(User vo) {
        System.out.println(vo.getId());
        System.out.println(vo.getUserName());
        System.out.println(vo.getCreateTime());
        System.out.println(vo.getUserSalary());
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

请求http://localhost:8080/api/users/testParam?userName=susq&Id=1&userPwd=123456&createTime=20170710&isDelete=1&userSalary=10000.123145 。输出

1
susq
123456
Mon Jul 10 00:00:00 CST 2017
1
10000.123145
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

也可以实现Formatter, Formatter是一类特殊的Converter,专门处理字符串的转换

类似于Converter, 修改实现和配置文件即可

public class DateConvert implements Formatter<Date> {

    @Override
    public Date parse(String s, Locale locale) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date d = null;
        try {
            d = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    @Override
    public String print(Date date, Locale locale) {
        return null;
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
    <bean id="dateConvert" class="com.susq.springbegin.utils.DateConvert"/>
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="formatters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值