SpringBoot自定義對(duì)象參數(shù)實(shí)現(xiàn)自動(dòng)類(lèi)型轉(zhuǎn)換與格式化
序章
問(wèn)題提出一:
當(dāng)我們用表單獲取一個(gè) Person 對(duì)象的所有屬性值時(shí), SpringBoot 是否可以直接根據(jù)這些屬性值將其轉(zhuǎn)換為 Person 對(duì)象
回答:
當(dāng)然可以,SpringBoot 通過(guò)自定義對(duì)象參數(shù),可以實(shí)現(xiàn)自動(dòng)類(lèi)型轉(zhuǎn)換與格式化,并可以級(jí)聯(lián)封裝(一個(gè)對(duì)象擁有另一個(gè)對(duì)象作為屬性時(shí),也可以封裝)。
一、實(shí)體類(lèi) Bean
person類(lèi)
注: 構(gòu)造方法一定要寫(xiě)全,無(wú)參數(shù)和有參數(shù)的都要寫(xiě),不然封裝過(guò)程會(huì)出問(wèn)題
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類(lèi)
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 屬性值要與類(lèi)的屬性名一一對(duì)應(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 類(lèi)
Post /saveuser 請(qǐng)求, 返回封裝好的 Person 類(lèi)
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é)果截圖

提出問(wèn)題二: SpringBoot 之所以可以自動(dòng)獲取表單值封裝為指定類(lèi)型對(duì)象,是因?yàn)镾pringBoot 具有嚴(yán)密的參數(shù)解析機(jī)制, 但是若我們的輸入值SpringBoot 不能解析時(shí),難道我們就只能坐以待斃了嘛
回答: 不是,我們可以通過(guò)WebMvcConfigurer定制化SpringMVC的功能,通過(guò)重寫(xiě) addFormatters 方法自定義類(lèi)型參數(shù)
示例
如下表單中的 “huahua,5個(gè)月” 字符串是不能被 SpringBoot 解析為 Pet 類(lèi)型的
<form action="/saveuser" method="post">
姓名: <input name="userName" value="liuwanqing"/> <br/>
年齡: <input name="age" value="20"/> <br/>
寵物:<input name="pet" value="huahua,5個(gè)月"/>
<input type="submit" value="保存">
</form>
自定義類(lèi)型參數(shù) 封裝POJO:
編寫(xiě)WebConfig類(lèi)實(shí)現(xiàn)WebMvcConfigurer類(lèi),重寫(xiě) 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自定義對(duì)象參數(shù)實(shí)現(xiàn)自動(dòng)類(lèi)型轉(zhuǎn)換與格式化的文章就介紹到這了,更多相關(guān)SpringBoot自定義對(duì)象參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot基礎(chǔ)入門(mén)之基于注解的Mybatis
這篇文章主要給大家介紹了關(guān)于Spring Boot基礎(chǔ)入門(mén)之基于注解的Mybatis的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
ApiOperation和ApiParam注解依賴(lài)的安裝和使用以及注意事項(xiàng)說(shuō)明
這篇文章主要介紹了ApiOperation和ApiParam注解依賴(lài)的安裝和使用以及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
SpringMVC HttpMessageConverter報(bào)文信息轉(zhuǎn)換器
??HttpMessageConverter???,報(bào)文信息轉(zhuǎn)換器,將請(qǐng)求報(bào)文轉(zhuǎn)換為Java對(duì)象,或?qū)ava對(duì)象轉(zhuǎn)換為響應(yīng)報(bào)文。???HttpMessageConverter???提供了兩個(gè)注解和兩個(gè)類(lèi)型:??@RequestBody,@ResponseBody???,??RequestEntity,ResponseEntity??2023-01-01
Java源碼分析:Guava之不可變集合ImmutableMap的源碼分析
今天給大家?guī)?lái)的是關(guān)于Java源碼的相關(guān)知識(shí),文章圍繞著Java ImmutableMap展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-06-06
idea2020.3測(cè)試評(píng)價(jià)及感受
idea2020.3版本這次變化最大的也就是 UI了完全拋棄了之前一直使用的模板更改成了新的樣式,感興趣的朋友快來(lái)下載體驗(yàn)下吧2020-10-10
Java基礎(chǔ)之方法重寫(xiě)和多態(tài)示例
這篇文章主要介紹了Java基礎(chǔ)之方法重寫(xiě)和多態(tài),結(jié)合實(shí)例形式分析了java方法重寫(xiě)和多態(tài)的相關(guān)原理與使用技巧,需要的朋友可以參考下2019-08-08
SpringCloud使用CircuitBreaker實(shí)現(xiàn)熔斷器的詳細(xì)步驟
在微服務(wù)架構(gòu)中,服務(wù)之間的依賴(lài)調(diào)用非常頻繁,當(dāng)一個(gè)下游服務(wù)因高負(fù)載或故障導(dǎo)致響應(yīng)變慢或不可用時(shí),可能會(huì)引發(fā)上游服務(wù)的級(jí)聯(lián)故障,最終導(dǎo)致整個(gè)系統(tǒng)崩潰,熔斷器是解決這類(lèi)問(wèn)題的關(guān)鍵模式之一,Spring Cloud提供了對(duì)熔斷器的支持,本文將詳細(xì)介紹如何集成和使用它2025-02-02

