- 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