为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Boot实战之定制type Formatters

本文首发于个人网站:Spring Boot实战之定制type Formatters

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

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

实战

  • 首先在项目根目录下创建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的定制功能需求。

我们在WebConfiguration中引入(@Autowired)了BookRepository(需要用它创建BookFormatter实例),Spring给配置文件提供了使用其他bean对象的能力。Spring本身会确保BookRepository先创建,然后在WebConfiguration类的创建过程中引入。

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

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
10

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消