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

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
主站蜘蛛池模板: 久治县| 盐山县| 利辛县| 封开县| 宁德市| 潞城市| 龙陵县| 古田县| 永顺县| 佛山市| 隆化县| 遂溪县| 沂水县| 剑河县| 石河子市| 绥滨县| 盘锦市| 卢湾区| 油尖旺区| 广西| 印江| 望谟县| 利川市| 亳州市| 丁青县| 陆良县| 廉江市| 东城区| 搜索| 上蔡县| 巴林右旗| 阿巴嘎旗| 屏山县| 莱州市| 集贤县| 县级市| 黄浦区| 郴州市| 永宁县| 三原县| 扶余县|