SpringMVC之@InitBinder注解(日期转换)

article/2025/10/7 13:32:19

@InitBinder注解的作用:

springmvc并不是能对所有类型的参数进行绑定的,如果对日期Date类型参数进行绑定,就会报错IllegalStateException错误。所以需要注册一些类型绑定器用于对参数进行绑定。InitBinder注解就有这个作用。


程序代码示例:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;@RestController
@RequestMapping("/date")
public class InitBinderController {@RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)private String testInitBinder(Date date) {System.out.println("date = " + date);return "RequsetInitBindDemo";}
}

postman测试:
在这里插入图片描述


不能把String类型转换为Date类型报错。

此时就需要一个日期类型转换器。

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;@RestController
@RequestMapping("/date")
public class InitBinderController {@RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)private String testInitBinder(Date date) {System.out.println("date = " + date);return "RequsetInitBindDemo";}@InitBinderpublic void dateTypeBinder(WebDataBinder webDataBinder) {//往数据绑定器中添加一个DateFormatter日期转化器。webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));}
}

postman测试:
在这里插入图片描述
打印结果:

date = Tue Jan 15 00:05:00 CST 2019


InitBinder注解源码:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InitBinder {//指定参数名,这个不知控制器方法上形参的参数名,而是请求参数名,//可以指定多个。指定后只有这些参数需要用到该转换器。如果不指定,默认所有。String[] value() default {};}

注意:并且使用InitBinder 注册的绑定器只有在当前Controller中才有效,不会作用于其他Controller。

此时,就需要用到@ControllerAdvice注解定义全局绑定器。使不同controller的方法都能作用到。

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;@ControllerAdvice
public class InitConfig {@InitBinderpublic void dateTypeBinder(WebDataBinder webDataBinder) {//往数据绑定器中添加一个DateFormatter日期转化器。webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));}
}

使用其他格式转化器

我们可以自定义格式转化器,实现Formatter接口就可。还可以添加验证器等等。

public class StringFormatter implements Formatter<String> {private static final String PREFIX = "convertString == ";@Overridepublic String parse(String text, Locale locale) throws ParseException {//所以String类型参数都加上一个前缀。String result = PREFIX + text;return result;}@Overridepublic String print(String object, Locale locale) {return object;}
}

添加:

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;@ControllerAdvice
public class InitConfig {@InitBinderpublic void dateTypeBinder(WebDataBinder webDataBinder) {//往数据绑定器中添加一个DateFormatter日期转化器。webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));//添加一个string类型的数据绑定器,作用是加个前缀webDataBinder.addCustomFormatter(new StringFormatter());}
}

测试:

@RequestMapping(value = "/testInitBinder2", method = RequestMethod.GET)private String testInitBinder2(String name) {System.out.println("name = " + name);return "RequsetInitBindDemo";}

在这里插入图片描述

打印结果:

name = convertString == 刘亦菲


http://chatgpt.dhexx.cn/article/i7kVsiOE.shtml

相关文章

initbinder对ajax不起作用,Spring MVC InitBinder验证方法

使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的&#xff0c;也即比较复杂的验证情况下才会使用。大部份简单的表单验证&#xff0c;使用annotation验证即可以解决。 这里需要注意的一点&#xff1a;InitBinder和Annotation两种验证只能二选…

SpringMvc @InitBinder

这篇博客记录InitBinder怎么起作用、起什么作用&#xff1f; 首先,该注解被解析的时机&#xff0c;是该匹配Controller的请求执行映射的方法之前; 同时 InitBinder标注的方法执行是多次的&#xff0c;一次请求来就执行一次。 当某个Controller上的第一次请求由SpringMvc前端控制…

java培训之InitBinder注解

InitBinder注解【了解】 InitBinder由 InitBinder 标识的方法&#xff0c;可以对 WebDataBinder 对象进行初始化。WebDataBinder 是 DataBinder 的子类&#xff0c;用于完成由表单字段到 JavaBean 属性的绑定InitBinder方法不能有返回值&#xff0c;它必须声明为void。InitBin…

SpringBoot @InitBinder注解绑定请求参数

参考资料 springMVC之InitBinder 和 ValidatorspringMVC之InitBinder的用法1springMVC之InitBinder的用法2 目录 一. 作用二. 前期准备三. Get请求 URL传值处理3.1 前台-test16.html3.2 Controller层3.3 效果 四. Post请求 表单传值 自定义日期属性绑定器4.1 前台-test16.h…

详细分析@InitBinder注解的使用和原理

前言 由InitBinder注解修饰的方法用于初始化WebDataBinder对象&#xff0c;能够实现&#xff1a;从request获取到handler方法中由RequestParam注解或PathVariable注解修饰的参数后&#xff0c;假如获取到的参数类型与handler方法上的参数类型不匹配&#xff0c;此时可以使用初…

SpringMVC之@InitBinder注解详解

说明与作用 springmvc并不是能对所有类型的参数进行绑定的&#xff0c;如果对日期Date类型参数进行绑定&#xff0c;就会报错IllegalStateException错误。所以需要注册一些类型绑定器用于对参数进行绑定。InitBinder注解就有这个作用。 Controller public class InitBinderCo…

SpringMVC中的@InitBinder注解【记录】

一、Spring请求参数绑定流程&#xff1a; 1、请求参数绑定流程&#xff1a; 我们在开发的时候&#xff0c;经常会从html&#xff0c;jsp中将请求参数通过request对象传递到后台&#xff0c;可是经常会遇到这么一种情况&#xff0c;那就是传过来的数据到后台后&#xff0c;还要…

springMVC之@InitBinder的用法

目录 一、InitBinder的作用二、数据绑定器三、全局数据绑定器3.1. 方式一&#xff1a;ControllerAdvice3.2. 方式二&#xff1a;RequestMappingHandlerAdapter 四、自定义数据校验器五、参数类型转换器 一、InitBinder的作用 InitBinder从字面意思可以看出这个的作用是给Binder…

JAVA-Switch语句

1、完整的语法结构 该语句为选择分支语句&#xff0c;其语法结构为&#xff1a; switch (值){case:值1 java语句;break;case:值2 java语句;break;case:值3 java语句;break;……default&#xff1a;java语句; } 注意在该语法结构中&#xff0c;“值N"可以表示int型的或者S…

java中的常用语句

Java中的常用语句 一、Java中的语句由3大类的结构 1.顺序结构—自上而下一行一行的有序的执行 2.选择结构 (1)If语句结构 (2)Switch语句结构 3.循环结构 (1)For循环 (2)While循环 (3)Do{}while()循环 二、判断语句中if语句的表现方式和用法 1.if(){} 2.if(){}else{} 3.if(){}e…

4.java中的常见语句

1.顺序结构语句 写好的代码从上往下按照顺序一行一行的执行。 2.选择结构语句 根据判断结果有选择性的执行代码. 2.1 if语句 1.if(判断条件){需要执行的java代码} 首先执行判断条件&#xff0c;如果判断条件的结果为true,就执行“{}”中的java代码&#x…

java基本语法(史上最全)

java基本语法&#xff08;史上最全&#xff09; &#xff08;一&#xff09;关键字和保留字 关键字的定义和特点 定义&#xff1a;被java语言赋予了特殊含义&#xff0c;用作专门用途的字符串。 特点&#xff1a;关键字中所有字母都为小写。关键字不能用作变量名&#xff0…

Linux Makefile ifeq正确使用

今晚和昨晚捣鼓了很久ifeq&#xff0c;怎么也得不出正确结果。当时我是这么用ifeq的 all: ifeq("ad","cd") echo yes else echo no endif 得出的结果是&#xff1a; 后来经仔细对比发现要这样写 all: ifeq ("ad", "cd&q…

关于shiro

shiro ​ shiro处理的两个过程&#xff0c;一个是登录&#xff0c;这个过程完成后产生一个用户jwt&#xff0c;一个是访问接口时&#xff0c;通过jwt来完成验证的过程 登录逻辑&#xff1a; 访问接口逻辑&#xff1a; 认证&#xff08;authentication&#xff09;&#xff1a…

shiro的简单介绍

1.Shiro的简单配置 1&#xff09; 获取ShiroFilterFactoryBean&#xff0c;作用是在执行相关操作前&#xff0c;先进行功能过滤&#xff0c;拦截所有请求&#xff0c;进入到shiro中进行认证与授权 例如&#xff1a;设置一些拦截的请求 // 身份认证失败&#xff0c;则跳转到登录…

Shiro相关基础知识

文章目录 前言一、Shiro相关基础知识1.Shiro是什么2.Shiro具体功能3.Shiro的整体结构与重要组件4.Shiro各模块基础知识1&#xff09;Authentication 认证模块2&#xff09;Authorization 授权模块3&#xff09;Realm 认证模块 5.Shiro集成到web应用 二、Shiro相关漏洞1. Shiro漏…

面试总结:Shiro框架

文章目录 Apache Shiro框架1. 简单介绍一下Shiro 框架2. Shiro 主要的四个组件3. Shiro 运行原理4. Shiro 的四种权限控制方式5. 授权实现的流程&#xff08;1&#xff09;、什么是粗颗粒和细颗粒权限&#xff1f;&#xff08;2&#xff09;、粗颗粒和细颗粒如何授权&#xff1…

shiro安全框架详解。面试必备

shiro核心就是过滤器。 认证授权流程&#xff1a; ● 认证&#xff1a;对用户的身份进行检查&#xff08;登录验证&#xff09; ● 授权&#xff1a;对用户的权限进行检查&#xff08;是否有对应的操作权限&#xff09; ● 流程图&#xff1a; 权限管理 实现权限的动态分配&a…

面试专题系列-Shiro

1.什么是shiro Apache Shiro 是 Java 的一个安全框架。使用 shiro 可以非常容易的开发出足够好的应用&#xff0c;其不仅可以用在 JavaSE环境&#xff0c;也可以用在 JavaEE 环境。Shiro 可以帮助我们完成&#xff1a;认证、授权、加密、会话管理、与 Web 集成、缓存等。 2.Sh…

shiro(详解)

这里写自定义目录标题 什么是shiro什么是权限管理什么是身份认证什么是授权SubjectSecurityManagerAuthenticatorAuthorizerRealmSessionManagerSessionDAOCacheManagerCryptography面试题认证的开发1. 创建项目并引入依赖2. 引入shiro配置文件并加入如下配置 自定义Realm自定义…