springboot接口入参的一些问题

springboot接口入参的一些问题

最近在工作中遇到一个接口入参类型转换错误未被处理的问题,于是整理了一些关于springmvc入参的问题

入参绑定

1、入参中我们最常见的是date类型的参数转换,这个可以通过注解来实现参数类型的转换,只需在bean对象的属性上方添加注解@DateTimeFormat(pattern=“yyyy-MM-dd”),pattern为时间对象的格式化
在这里插入图片描述

2、在controller类里定义数据绑定类

/**
     * 在controller层中加入一段数据绑定代码
     * @param webDataBinder
     */
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
    }

3、定义全局的参数类型转换器
首先建立一个实现Converter的转换器

 public class DateConverter implements Converter&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值