Spring Boot項目中定制PropertyEditors方法
在Spring Boot: 定制HTTP消息轉(zhuǎn)換器一文中我們學(xué)習(xí)了如何配置消息轉(zhuǎn)換器用于HTTP請求和響應(yīng)數(shù)據(jù),實際上,在一次請求的完成過程中還發(fā)生了其他的轉(zhuǎn)換,我們這次關(guān)注將參數(shù)轉(zhuǎn)換成多種類型的對象,如:字符串轉(zhuǎn)換成Date對象或字符串轉(zhuǎn)換成Integer對象。
在編寫控制器中的action方法時,Spring允許我們使用具體的數(shù)據(jù)類型定義函數(shù)簽名,這是通過PropertyEditor實現(xiàn)的。PropertyEditor本來是JDK提供的API,用于將文本值轉(zhuǎn)換成給定的類型,結(jié)果Spring的開發(fā)人員發(fā)現(xiàn)它恰好滿足Spring的需求——將URL參數(shù)轉(zhuǎn)換成函數(shù)的參數(shù)類型。
針對常用的類型(Boolean、Currency和Class),Spring MVC已經(jīng)提供了很多PropertyEditor實現(xiàn)。假設(shè)我們需要創(chuàng)建一個Isbn類并用它作為函數(shù)中的參數(shù)。
實戰(zhàn)
考慮到PropertyEditor屬于工具范疇,選擇在項目根目錄下增加一個包——utils。在這個包下定義Isbn類和IsbnEditor類,各自代碼如下:
Isbn類:
package com.test.bookpub.utils; public class Isbn { private String isbn; public Isbn(String isbn) { this.isbn = isbn; } public String getIsbn() { return isbn; } }
IsbnEditor類,繼承PropertyEditorSupport類,setAsText完成字符串到具體對象類型的轉(zhuǎn)換,getAsText完成具體對象類型到字符串的轉(zhuǎn)換。
package com.test.bookpub.utils; import org.springframework.util.StringUtils; import java.beans.PropertyEditorSupport; public class IsbnEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { setValue(new Isbn(text.trim())); } else { setValue(null); } } @Override public String getAsText() { Isbn isbn = (Isbn) getValue(); if (isbn != null) { return isbn.getIsbn(); } else { return ""; } } }
在BookController中增加initBinder函數(shù),通過@InitBinder注解修飾,則可以針對每個web請求創(chuàng)建一個editor實例。
@InitBinderpublic void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Isbn.class, new IsbnEditor()); }
修改BookController中對應(yīng)的函數(shù)
@RequestMapping(value = "/{isbn}", method = RequestMethod.GET) public Map<String, Object> getBook(@PathVariable Isbn isbn) { Book book = bookRepository.findBookByIsbn(isbn.getIsbn()); Map<String, Object> response = new LinkedHashMap<>(); response.put("message", "get book with isbn(" + isbn.getIsbn() +")"); response.put("book", book); return response; }
運行程序,通過Httpie訪問http localhost:8080/books/9781-1234-1111,可以得到正常結(jié)果,跟之前用String表示isbn時沒什么不同,說明我們編寫的IsbnEditor已經(jīng)起作用了。
分析
Spring提供了很多默認(rèn)的editor,我們也可以通過繼承PropertyEditorSupport實現(xiàn)自己定制化的editor。
由于ProperteyEditor是非線程安全的。通過@InitBinder注解修飾的initBinder函數(shù),會為每個web請求初始化一個editor實例,并通過WebDataBinder對象注冊。
相關(guān)文章
SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程
這篇文章主要介紹了SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02Java中forward轉(zhuǎn)發(fā)與redirect重定向的區(qū)別
轉(zhuǎn)發(fā)和重定向都是常用的頁面跳轉(zhuǎn)方式,但在實現(xiàn)上有一些區(qū)別,本文主要介紹了Java中forward轉(zhuǎn)發(fā)與redirect重定向的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2023-11-11