Spring Boot实战之定制type Formatters

本文首发于我的网站:Spring Boot实战之定制type Formattersjava

前面咱们有篇文章介绍了PropertyEditors,是用来将文本类型转换成指定的Java类型,不过,考虑到PropertyEditor的无状态和非线程安全特性,Spring 3增长了一个Formatter接口来替代它。Formatters提供和PropertyEditor相似的功能,可是提供线程安全特性,也能够实现字符串和对象类型的互相转换。面试

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

实战

  • 首先在项目根目录下建立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();
}
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

[]

分析

Formatter工具的目标是提供跟PropertyEditor相似的功能。经过FormatterRegistry将咱们本身的formtter注册到系统中,而后Spring会自动完成文本表示的book和book实体对象之间的互相转换。因为Formatter是无状态的,所以不须要为每一个请求都执行注册formatter的动做。数据库

**使用建议:**若是须要通用类型的转换——例如String或Boolean,最好使用PropertyEditor完成,由于这种需求可能不是全局须要的,只是某个Controller的定制功能需求。json

咱们在WebConfiguration中引入(@Autowired)了BookRepository(须要用它建立BookFormatter实例),Spring给配置文件提供了使用其余bean对象的能力。Spring自己会确保BookRepository先建立,而后在WebConfiguration类的建立过程当中引入。后端

Spring Boot 1.x系列

  1. Spring Boot的自动配置、Command-line-Runner
  2. 了解Spring Boot的自动配置
  3. Spring Boot的@PropertySource注解在整合Redis中的使用
  4. Spring Boot项目中如何定制HTTP消息转换器
  5. Spring Boot整合Mongodb提供Restful接口
  6. Spring中bean的scope
  7. Spring Boot项目中使用事件派发器模式
  8. Spring Boot提供RESTful接口时的错误处理实践
  9. Spring Boot实战之定制本身的starter
  10. Spring Boot项目如何同时支持HTTP和HTTPS协议
  11. 自定义的Spring Boot starter如何设置自动配置注解
  12. Spring Boot项目中使用Mockito
  13. 在Spring Boot项目中使用Spock测试框架
  14. Spring Boot项目中如何定制拦截器
  15. Spring Boot项目中如何定制PropertyEditors
  16. Spring Boot构建的Web项目如何在服务端校验表单输入
  17. Spring Boot应用的健康监控
  18. Spring Boot项目中如何定制servlet-filters
  19. Spring Boot实战之定制URL匹配规则

本号专一于后端技术、JVM问题排查和优化、Java面试题、我的成长和自我管理等主题,为读者提供一线开发者的工做和成长经验,期待你能在这里有所收获。javaadu安全

相关文章
相关标签/搜索