SpringBoot @InitBinder注解绑定请求参数

article/2025/10/7 14:31:44

参考资料

  1. springMVC之@InitBinder 和 Validator
  2. springMVC之@InitBinder的用法1
  3. springMVC之@InitBinder的用法2

目录

  • 一. 作用
  • 二. 前期准备
  • 三. Get请求 + URL传值处理
    • 3.1 前台-test16.html
    • 3.2 Controller层
    • 3.3 效果
  • 四. Post请求 + 表单传值 + 自定义日期属性绑定器
    • 4.1 前台-test16.html
    • 4.2 form实体类
    • 4.3 Controller层
    • 4.4 效果
  • 五. 其他自定义属性编辑器实例
    • 5.1 自定义SexPropertyEditor
    • 5.2 自定义StringToListPropertyEditor
    • 5.3 form实体类
    • 5.4 前端
    • 5.5 Controller层
    • 5.6 效果
  • 六. 多个@InitBinder注解修饰的方法
  • 七. 其他用法


一. 作用

作用于Controller层中,在Controller层的方法执行前执行,主要作用是初始化当前Controller层的数据绑定器(或者属性绑定器),帮助完成数据处理和数据绑定。

被该注解修饰的方法会有一个形参WebDataBinder,可以帮我们将request请求中的参数处理绑定到JavaBean中。

在这里插入图片描述

二. 前期准备

import lombok.Data;import java.math.BigDecimal;
import java.util.Date;@Data
public class Test16Form {private String name;private String sex;private Date birthday;private BigDecimal money;
}

三. Get请求 + URL传值处理

3.1 前台-test16.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<div><button id="getBtn">发送get请求</button><br>
</div>
</body>
<script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}"></script>
<script>$("#getBtn").click(function() {const urlSearchParams = new URLSearchParams();// 👉含有空格urlSearchParams.append("name", "贾飞天   ");urlSearchParams.append("sex", "男");// 👉值为yyyy-MM-dd HH:mm:ss格式的日期字符串urlSearchParams.append("birthday", "2022-11-11 12:12:12");urlSearchParams.append("money", "10000");const url = `/test16/receiveGet?${urlSearchParams.toString()}`;$.ajax({url,type: 'GET',success: function (data, status, xhr) {console.log(data);}});});
</script>
</html>

3.2 Controller层

  • StringTrimmerEditorCustomDateEditor是框架自带的属性处理器
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;@Controller
@RequestMapping("/test16")
public class Test16Controller {@InitBinderpublic void formBinder(WebDataBinder binder) {// 只要是String类型,就去除字符串前后的空格binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));// 只有当属性名为birthday且为Date类型的才使用使用框架自带的CustomDateEditor编辑器将String处理为DateDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");binder.registerCustomEditor(Date.class, "birthday", new CustomDateEditor(df, true));}@GetMapping("/init")public ModelAndView init() {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("test16");return modelAndView;}@GetMapping("/receiveGet")@ResponseBodypublic void receiveGet(Test16Form form) {System.out.println(form);}
}

3.3 效果

  • 字符串两端的空格被去除
  • String格式的日期被转换为Date格式的日期
    在这里插入图片描述

四. Post请求 + 表单传值 + 自定义日期属性绑定器

4.1 前台-test16.html

  • 表单提交的数据若包含List<实体类>这种数据结构
    在前台需要用 form对应的属性名[下标].实体类属性名 这种方式准备数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<div><button id="postBtn">发送post请求</button><br>
</div>
</body>
<script type="text/javascript" th:src="@{/js/public/jquery-3.6.0.min.js}"></script>
<script>$("#postBtn").click(function() {const paramObj = {name: "贾飞天    ",sex: '不明',money: "10000",// yyyy-MM-dd HH:mm:ss格式birthday: '2022-11-11 12:12:12',// 后台中的List实体类区域"tableList[0].id": 1,"tableList[0].address": '测试address    ',"tableList[0].hobby": '测试hobby     ',// yyyy年MM月dd日 HH:mm:ss格式"tableList[0].workDate": '2022年11月11日 14:14:14',};$.ajax({url: `/test16/receivePost`,type: 'POST',data: paramObj,// 表单格式提交contentType : 'application/x-www-form-urlencoded;charset=utf-8',// 后端返回给前端的数据类型dataType: 'json',success: function (data, status, xhr) {console.log(data);}});});
</script>
</html>

4.2 form实体类

import lombok.Data;import java.math.BigDecimal;
import java.util.Date;
import java.util.List;@Data
public class Test16Form {private String name;private String sex;// 待转换类型private Date birthday;private BigDecimal money;private List<Test4Entity> tableList;
}
import lombok.Data;
import java.util.Date;@Data
public class Test4Entity {private String id;private String address;private String hobby;// 待转换类型private Date workDate;
}

4.3 Controller层

  • 我们可以通过PropertyEditorSupport类来实现我们自己的属性编辑器
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;@Controller
@RequestMapping("/test16")
public class Test16Controller {@InitBinderpublic void formBinder(WebDataBinder binder) {// 只要是String类型,就去除字符串前后的空格binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));// 自定义日期转换属性处理器binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {@Overridepublic void setAsText(String dateStr) {DateFormat dateFormat = null;try {if (ObjectUtils.isEmpty(dateStr)) {setValue(dateStr);return;}// yyyy-MM-dd HH:mm:ss格式if (dateStr.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");}// yyyy年MM月dd日 HH:mm:ss格式else if (dateStr.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");}if (ObjectUtils.isEmpty(dateFormat)) {setValue(null);return;}Date parse = dateFormat.parse(dateStr);setValue(parse);} catch (Exception ex) {setValue(null);}}});}@GetMapping("/init")public ModelAndView init() {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("test16");return modelAndView;}@PostMapping("/receivePost")@ResponseBodypublic void receivePost(Test16Form form) {System.out.println(form);}
}

4.4 效果

  • 可以看到 yyyy-MM-dd HH:mm:ss 和 yyyy年MM月dd日 HH:mm:ss 格式的字符串日期都被转换为Date数类型
  • 因为没有指定转换特定的属性名所对应的数据,所以包括一览中的数据也被成功转换
  • 一览中的字符换的前后空白也被清除,一览中的日期格式的也被成功转换
    在这里插入图片描述

五. 其他自定义属性编辑器实例

5.1 自定义SexPropertyEditor

  • 对性别进行编辑,如果性别为空或者不为男性或者女性,默认设置为男性
import org.springframework.util.ObjectUtils;import java.beans.PropertyEditorSupport;
import java.util.Arrays;
import java.util.List;public class SexPropertyEditor extends PropertyEditorSupport {private final static List<String> sexList = Arrays.asList("男", "女");@Overridepublic void setAsText(String sex) {// 当性别为空或者不是男或女的时候,默认设置为男性if(ObjectUtils.isEmpty(sex) || !sexList.contains(sex)) {setValue("男");return;}setValue(sex);}
}

5.2 自定义StringToListPropertyEditor

  • 将参数中的属性名=XXX-XXX-XXX的数据转换为数组
import org.springframework.util.ObjectUtils;import java.beans.PropertyEditorSupport;public class StringToListPropertyEditor extends PropertyEditorSupport {@Overridepublic void setAsText(String text){if (ObjectUtils.isEmpty(text) || !text.contains("-")) {setValue(text);return;}setValue(text.split("-"));}
}

5.3 form实体类

  • Test16Form01.java
import lombok.Data;@Data
public class Test16Form01 {private String sex;private String[] numList;private String[] addList;
}

5.4 前端

const url = `/test16/receiveNumListAndSex?sex=不明&numList=1-2-3&addList=4-5-6`;
$.ajax({url,type: 'GET',success: function (data, status, xhr) {console.log(data);}
});

5.5 Controller层

@Controller
@RequestMapping("/test16")
public class Test16Controller {@InitBinderpublic void formBinder(WebDataBinder binder) {// 当数据类型为String[],且 属性名为 numList 的时候才会起作用// 虽然addList也是String[]格式的数据,但是我们并没有指定转换此属性binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor());// 当数据类型为String 且 属性名为 sex 的时候才会起作用binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor());}@GetMapping("/receiveNumListAndSex")@ResponseBodypublic void receiveNumList(Test16Form01 form) {System.out.println(form);}
}

5.6 效果

在这里插入图片描述

六. 多个@InitBinder注解修饰的方法

  • 如果@InitBinder注解没有添加value值,则每个请求都会走被其修饰的方法
  • 如果@InitBinder注解有value值,则只有参数的名称与其相同才会走此方法
import com.example.jmw.common.bindEditor.SexPropertyEditor;
import com.example.jmw.common.bindEditor.StringToListPropertyEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;@Controller
@RequestMapping("/test16")
public class Test16Controller {// 注解没有添加value值,每个请求都会走此方法@InitBinderpublic void init(WebDataBinder binder) {binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));}// 指定只有参数名称为test16Form01的,才会走此方法@InitBinder("test16Form01")public void formBinder(WebDataBinder binder) {// 当数据类型为String 且 属性名为 sex 的时候才会起作用binder.registerCustomEditor(String.class, "sex", new SexPropertyEditor());}// 指定只有参数名称为test16Form的,才会走此方法@InitBinder("test16Form")public void receiveGetBinder(WebDataBinder binder) {// 当数据类型为String[],且 属性名为 numList 的时候才会起作用binder.registerCustomEditor(String[].class, "numList", new StringToListPropertyEditor());}@GetMapping("/receiveGet")@ResponseBodypublic void receiveGet(Test16Form form) {System.out.println(form);}@GetMapping("/receiveNumListAndSex")@ResponseBodypublic void receiveNumList(Test16Form01 form) {System.out.println(form);}
}

在这里插入图片描述

七. 其他用法

  1. 当前Controller继承父类,在父类中使用@InitBinder注解来修饰的方法
  2. 配合@ControllerAdvice注解作用于全局,具体用法请参考 顶部的参考资料

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

相关文章

详细分析@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自定义…

【JAVA面试题整理】框架之Shiro

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

shiro总结

shiro主要内容: 1:SecurityUtils shiro提供的工具类,主要作用是获取 SecurityManager和Subject public abstract class SecurityUtils {private static SecurityManager securityManager;//获取Subjectpublic static Subject getSubject() {Subject subject ThreadContext.…

Java面试系列总结 :Shiro

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

Shiro知识总结二

3. 与 Spring Boot 整合 3.1 框架整合 依赖 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>o…