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

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
主站蜘蛛池模板: 正宁县| 太白县| 安阳县| 宜兴市| 漠河县| 隆德县| 华安县| 东山县| 上杭县| 英超| 西和县| 舞阳县| 茶陵县| 临城县| 苏尼特右旗| 长沙县| 岫岩| 平度市| 乌鲁木齐县| 福安市| 嫩江县| 拜城县| 安顺市| 安仁县| 奉节县| 沙洋县| 湖口县| 育儿| 曲松县| 镇江市| 唐河县| 大同市| 醴陵市| 仁寿县| 房产| 沧源| 万全县| 修武县| 临桂县| 乌鲁木齐市| 阿图什市|