官术网_书友最值得收藏!

How to do it...

  1. 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
  2. Let's create a new package called model under the src/main/java/com/example/bookpub directory from the root of our project
  3. 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;
} }
  1. Let's create a new package called editors under the src/main/java/com/example/bookpub directory from the root of our project
  2. 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() : "");
}
}
  1. 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()); 
} 
  1. 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()); }
  1. Start the application by running ./gradlew clean bootRun
  2. In the browser, go to http://localhost:8080/books/978-1-78528-415-1
  3. 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
主站蜘蛛池模板: 汾西县| 丰都县| 汉中市| 新营市| 重庆市| 祁东县| 正宁县| 长汀县| 神木县| 鹿泉市| 溧水县| 定陶县| 辰溪县| 朝阳区| 涞水县| 辽源市| 吉水县| 临江市| 临海市| 建宁县| 开封县| 太康县| 吉安县| 舒兰市| 五原县| 普兰县| 阿拉善盟| 青浦区| 无极县| 图木舒克市| 康马县| 莱西市| 安塞县| 惠东县| 伊宁县| 汶川县| 永济市| 溆浦县| 庄浪县| 扶沟县| 西充县|