- Spring Boot 2.0 Cookbook(Second Edition)
- Alex Antonov
- 466字
- 2021-06-24 19:24:42
How to do it...
- First, we will need to remove the extendMessageConverters method from our WebConfiguration class as the converters.clear() call will break the rendering because we removed all of the supported type converters
- Let's create a new package called model under the src/main/java/com/example/bookpub directory from the root of our project
- Next we create a class named Isbn under our newly created package directory from the root of our project with the following content:
package com.example.bookpub.model; import org.springframework.util.Assert; public class Isbn { private String eanPrefix; private String registrationGroup; private String registrant; private String publication; private String checkDigit; public Isbn(String eanPrefix, String registrationGroup, String registrant, String publication, String checkDigit) { this.eanPrefix = eanPrefix; this.registrationGroup = registrationGroup; this.registrant = registrant; this.publication = publication; this.checkDigit = checkDigit; } public String getEanPrefix() { return eanPrefix; } public void setEanPrefix(String eanPrefix) { this.eanPrefix = eanPrefix; } public String getRegistrationGroup() { return registrationGroup; } public void setRegistrationGroup
(String registrationGroup) { this.registrationGroup = registrationGroup; } public String getRegistrant() { return registrant; } public void setRegistrant(String registrant) { this.registrant = registrant; } public String getPublication() { return publication; } public void setPublication(String publication) { this.publication = publication; } public String getCheckDigit() { return checkDigit; } public void setCheckDigit(String checkDigit) { this.checkDigit = checkDigit; } public static Isbn parseFrom(String isbn) { Assert.notNull(isbn); String[] parts = isbn.split("-"); Assert.state(parts.length == 5); Assert.noNullElements(parts); return new Isbn(parts[0], parts[1], parts[2], parts[3], parts[4]); } @Override
public String toString() {
return eanPrefix + '-'
+ registrationGroup + '-'
+ registrant + '-'
+ publication + '-'
+ checkDigit;
} }
- Let's create a new package called editors under the src/main/java/com/example/bookpub directory from the root of our project
- Let's create a class named IsbnEditor under our newly created package directory from the root of our project with the following content:
package com.example.bookpub.editors;
import org.springframework.util.StringUtils;
import com.example.bookpub.model.Isbn;
import java.beans.PropertyEditorSupport;
public class IsbnEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) {
if (text == null) {
setValue(null);
}
else {
String value = text.trim();
if (!StringUtils.isEmpty(value)) {
setValue(Isbn.parseFrom(value));
} else {
setValue(null);
}
}
}
@Override
public String getAsText() {
Object value = getValue();
return (value != null ? value.toString() : "");
}
}
- Next, we will add a method, initBinder, to BookController where we will configure the IsbnEditor method with the following content:
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Isbn.class, new IsbnEditor()); }
- Our getBook method in BookController will also change in order to accept the Isbn object, in the following way:
@RequestMapping(value = "/{isbn}", method = RequestMethod.GET) public Book getBook(@PathVariable Isbn isbn) {
return bookRepository.findBookByIsbn(isbn.toString()); }
- Start the application by running ./gradlew clean bootRun
- In the browser, go to http://localhost:8080/books/978-1-78528-415-1
- While we will not observe any visible changes, IsbnEditor is indeed at work, creating an instance of an Isbn class object from the {isbn} parameter
推薦閱讀
- 數據要素安全流通
- Hands-On Machine Learning with Microsoft Excel 2019
- 達夢數據庫編程指南
- Modern Programming: Object Oriented Programming and Best Practices
- 軟件成本度量國家標準實施指南:理論、方法與實踐
- 跟老男孩學Linux運維:MySQL入門與提高實踐
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設計
- 淘寶、天貓電商數據分析與挖掘實戰(第2版)
- 區塊鏈技術應用與實踐案例
- 菜鳥學SPSS數據分析
- 改變未來的九大算法
- 數據庫原理與設計實驗教程(MySQL版)
- 云原生架構:從技術演進到最佳實踐
- SQL Server 數據庫教程(2008版)
- 工業大數據分析實踐