SpringBoot中如何自定义对象参数实现自动类型转换及格式化处理?

2026-05-25 21:051阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计984个文字,预计阅读时间需要4分钟。

SpringBoot中如何自定义对象参数实现自动类型转换及格式化处理?

目录+章节+一、实体类Bean+二、前端页面index.+三、Controller类+四、运行结果截图+五、问题提出:当我们需要用表单获取一个Person对象的全部属性值时,SpringBoot是否可以直接根据数据?

目录
  • 序章
  • 一、实体类 Bean
  • 二、前端表单index.html
  • 三、Controller 类
  • 四、运行结果截图

序章

问题提出一:

当我们用表单获取一个 Person 对象的所有属性值时, SpringBoot 是否可以直接根据这些属性值将其转换为 Person 对象

回答:

当然可以,SpringBoot 通过自定义对象参数,可以实现自动类型转换与格式化,并可以级联封装(一个对象拥有另一个对象作为属性时,也可以封装)。

一、实体类 Bean

person类

注: 构造方法一定要写全,无参数和有参数的都要写,不然封装过程会出问题

import org.springframework.context.annotation.Bean; import javax.xml.crypto.Data; public class Person { String userName; int age; Pet pet; public Person() { } public Person(String userName, int age, Pet pet) { this.userName = userName; this.age = age; this.pet = pet; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } }

pet类

package com.example.demo2.bean; public class Pet { String name; String age; public Pet() { } public Pet(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }

二、前端表单index.html

注意 input 的 name 属性值要与类的属性名一一对应

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自定义参数绑定原理</title> </head> <body> <form action="/saveuser" method="post"> 姓名: <input name="userName" value="liuwanqing"/> <br/> 年龄: <input name="age" value="20"/> <br/> 宠物姓名:<input name="pet.name"/><br/> 宠物年龄:<input name="pet.age" /> <input type="submit" value="保存"> </form> </body> </html>

三、Controller 类

Post /saveuser 请求, 返回封装好的 Person 类

package com.example.demo2.controller; import com.example.demo2.bean.Person; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ParameterTestController { @PostMapping("/saveuser") public Person saveuser(Person person){ return person; } }

四、运行结果截图

提出问题二: SpringBoot 之所以可以自动获取表单值封装为指定类型对象,是因为SpringBoot 具有严密的参数解析机制, 但是若我们的输入值SpringBoot 不能解析时,难道我们就只能坐以待毙了嘛

回答: 不是,我们可以通过WebMvcConfigurer定制化SpringMVC的功能,通过重写 addFormatters 方法自定义类型参数

示例

如下表单中的 “huahua,5个月” 字符串是不能被 SpringBoot 解析为 Pet 类型的

<form action="/saveuser" method="post"> 姓名: <input name="userName" value="liuwanqing"/> <br/> 年龄: <input name="age" value="20"/> <br/> 宠物:<input name="pet" value="huahua,5个月"/> <input type="submit" value="保存"> </form>

自定义类型参数 封装POJO:

编写WebConfig类实现WebMvcConfigurer类,重写 addFormatters 方法

package com.example.demo2.config; import com.example.demo2.bean.Pet; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistry; import org.springframework.util.StringUtils; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer{ //1、WebMvcConfigurer定制化SpringMVC的功能 @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new Converter<String, Pet>() { @Override public Pet convert(String source) { if(!StringUtils.isEmpty(source)){ Pet pet = new Pet(); String[] split = source.split(","); pet.setName(split[0]); pet.setAge(split[1]); return pet; } return null; } }); } }; } }

到此这篇关于SpringBoot自定义对象参数实现自动类型转换与格式化的文章就介绍到这了,更多相关SpringBoot自定义对象参数内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

SpringBoot中如何自定义对象参数实现自动类型转换及格式化处理?

本文共计984个文字,预计阅读时间需要4分钟。

SpringBoot中如何自定义对象参数实现自动类型转换及格式化处理?

目录+章节+一、实体类Bean+二、前端页面index.+三、Controller类+四、运行结果截图+五、问题提出:当我们需要用表单获取一个Person对象的全部属性值时,SpringBoot是否可以直接根据数据?

目录
  • 序章
  • 一、实体类 Bean
  • 二、前端表单index.html
  • 三、Controller 类
  • 四、运行结果截图

序章

问题提出一:

当我们用表单获取一个 Person 对象的所有属性值时, SpringBoot 是否可以直接根据这些属性值将其转换为 Person 对象

回答:

当然可以,SpringBoot 通过自定义对象参数,可以实现自动类型转换与格式化,并可以级联封装(一个对象拥有另一个对象作为属性时,也可以封装)。

一、实体类 Bean

person类

注: 构造方法一定要写全,无参数和有参数的都要写,不然封装过程会出问题

import org.springframework.context.annotation.Bean; import javax.xml.crypto.Data; public class Person { String userName; int age; Pet pet; public Person() { } public Person(String userName, int age, Pet pet) { this.userName = userName; this.age = age; this.pet = pet; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } }

pet类

package com.example.demo2.bean; public class Pet { String name; String age; public Pet() { } public Pet(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }

二、前端表单index.html

注意 input 的 name 属性值要与类的属性名一一对应

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自定义参数绑定原理</title> </head> <body> <form action="/saveuser" method="post"> 姓名: <input name="userName" value="liuwanqing"/> <br/> 年龄: <input name="age" value="20"/> <br/> 宠物姓名:<input name="pet.name"/><br/> 宠物年龄:<input name="pet.age" /> <input type="submit" value="保存"> </form> </body> </html>

三、Controller 类

Post /saveuser 请求, 返回封装好的 Person 类

package com.example.demo2.controller; import com.example.demo2.bean.Person; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ParameterTestController { @PostMapping("/saveuser") public Person saveuser(Person person){ return person; } }

四、运行结果截图

提出问题二: SpringBoot 之所以可以自动获取表单值封装为指定类型对象,是因为SpringBoot 具有严密的参数解析机制, 但是若我们的输入值SpringBoot 不能解析时,难道我们就只能坐以待毙了嘛

回答: 不是,我们可以通过WebMvcConfigurer定制化SpringMVC的功能,通过重写 addFormatters 方法自定义类型参数

示例

如下表单中的 “huahua,5个月” 字符串是不能被 SpringBoot 解析为 Pet 类型的

<form action="/saveuser" method="post"> 姓名: <input name="userName" value="liuwanqing"/> <br/> 年龄: <input name="age" value="20"/> <br/> 宠物:<input name="pet" value="huahua,5个月"/> <input type="submit" value="保存"> </form>

自定义类型参数 封装POJO:

编写WebConfig类实现WebMvcConfigurer类,重写 addFormatters 方法

package com.example.demo2.config; import com.example.demo2.bean.Pet; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistry; import org.springframework.util.StringUtils; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer{ //1、WebMvcConfigurer定制化SpringMVC的功能 @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new Converter<String, Pet>() { @Override public Pet convert(String source) { if(!StringUtils.isEmpty(source)){ Pet pet = new Pet(); String[] split = source.split(","); pet.setName(split[0]); pet.setAge(split[1]); return pet; } return null; } }); } }; } }

到此这篇关于SpringBoot自定义对象参数实现自动类型转换与格式化的文章就介绍到这了,更多相关SpringBoot自定义对象参数内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

SpringBoot中如何自定义对象参数实现自动类型转换及格式化处理?