集册 Spring Boot 入门实践 Spring Boot:定制type Formatters

Spring Boot:定制type Formatters

欢马劈雪     最近更新时间:2020-08-04 05:37:59

681

考虑到PropertyEditor的无状态和非线程安全特性,Spring 3增加了一个Formatter接口来替代它。Formatters提供和PropertyEditor类似的功能,但是提供线程安全特性,并且专注于完成字符串和对象类型的互相转换。

假设在我们的程序中,需要根据一本书的ISBN字符串得到对应的book对象。通过这个类型格式化工具,我们可以在控制器的方法签名中定义Book参数,而URL参数只需要包含ISBN号和数据库ID。

How Do

  • 首先在项目根目录下创建formatters
  • 然后创建BookFormatter,它实现了Formatter接口,实现两个函数:parse用于将字符串ISBN转换成book对象;print用于将book对象转换成该book对应的ISBN字符串。
package com.test.bookpub.formatters;

import com.test.bookpub.domain.Book;
import com.test.bookpub.repository.BookRepository;
import org.springframework.format.Formatter;
import java.text.ParseException;
import java.util.Locale;

public class BookFormatter implements Formatter<Book> {
    private BookRepository repository;

    public BookFormatter(BookRepository repository) {
        this.repository = repository;
    }
    @Override
    public Book parse(String bookIdentifier, Locale locale) throws ParseException {
        Book book = repository.findBookByIsbn(bookIdentifier);
        return book != null ? book : repository.findOne(Long.valueOf(bookIdentifier));
    }
    @Override
    public String print(Book book, Locale locale) {
        return book.getIsbn();
    }
}
  • 在WebConfiguration中添加我们定义的formatter,重写(@Override修饰)addFormatter(FormatterRegistry registry)函数。
@Autowired
private BookRepository bookRepository;
@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addFormatter(new BookFormatter(bookRepository));
}
  • 最后,需要在BookController中新加一个函数getReviewers,根据一本书的ISBN号获取该书的审阅人。
@RequestMapping(value = "/{isbn}/reviewers", method = RequestMethod.GET)
public List<Reviewer> getReviewers(@PathVariable("isbn") Book book) {
    return book.getReviewers();
}
  • 通过mvn spring-boot:run运行程序
  • 通过httpie访问URL——http://localhost:8080/books/9781-1234-1111/reviewers,得到的结果如下:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Date: Tue, 08 Dec 2015 08:15:31 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked

[]

分析

展开阅读全文