SpringBoot自定義對象參數(shù)超詳細(xì)介紹作用
問題提出一:
當(dāng)我們用表單獲取一個 Person 對象的所有屬性值時, SpringBoot 是否可以直接根據(jù)這些屬性值將其轉(zhuǎn)換為 Person 對象
回答:
當(dāng)然可以,SpringBoot 通過自定義對象參數(shù),可以實(shí)現(xiàn)自動類型轉(zhuǎn)換與格式化,并可以級聯(lián)封裝(一個對象擁有另一個對象作為屬性時,也可以封裝)。
一、實(shí)體類 Bean
person類
注: 構(gòu)造方法一定要寫全,無參數(shù)和有參數(shù)的都要寫,不然封裝過程會出問題
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 屬性值要與類的屬性名一一對應(yīng)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定義參數(shù)綁定原理</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;
}
}四、運(yùn)行結(jié)果截圖

提出問題二: SpringBoot 之所以可以自動獲取表單值封裝為指定類型對象,是因?yàn)镾pringBoot 具有嚴(yán)密的參數(shù)解析機(jī)制, 但是若我們的輸入值SpringBoot 不能解析時,難道我們就只能坐以待斃了嘛
回答: 不是,我們可以通過WebMvcConfigurer定制化SpringMVC的功能,通過重寫 addFormatters 方法自定義類型參數(shù)
示例
如下表單中的 “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>自定義類型參數(shù) 封裝POJO:
編寫WebConfig類實(shí)現(xiàn)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;
}
});
}
};
}
}到此這篇關(guān)于SpringBoot自定義對象參數(shù)超詳細(xì)介紹作用的文章就介紹到這了,更多相關(guān)SpringBoot自定義對象參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA啟動tomcat控制臺中文亂碼問題的解決方法(100%有效)
很多人在idea中啟動項(xiàng)目時會出現(xiàn)控制臺的中文亂碼,其實(shí)也無傷大雅,但是本人看著不舒服,下面這篇文章主要給大家介紹了關(guān)于IDEA啟動tomcat控制臺中文亂碼問題的解決方法,需要的朋友可以參考下2022-09-09
SpringBoot自定義路由覆蓋實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringBoot自定義路由覆蓋實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
Java Kafka 消費(fèi)積壓監(jiān)控的示例代碼
這篇文章主要介紹了Java Kafka 消費(fèi)積壓監(jiān)控,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
Java數(shù)據(jù)庫連接池之DBCP淺析_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)庫連接池之DBCP的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
詳解SpringCloud Ribbon 負(fù)載均衡通過服務(wù)器名無法連接的神坑
這篇文章主要介紹了詳解SpringCloud Ribbon 負(fù)載均衡通過服務(wù)器名無法連接的神坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
springboot + vue 實(shí)現(xiàn)遞歸生成多級菜單(實(shí)例代碼)
這篇文章主要介紹了springboot + vue 實(shí)現(xiàn)遞歸生成多級菜單,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12

