格式化转换器(Springboot强大的数据格式化功能你必须要知道)

环境:Springboot2.4.11


通常,当需要实现通用类型转换逻辑时,可以使用转换器SPI 例如,用于在java.util.Date和Long之间转换。当你在客户端环境(如web应用程序)中工作并且需要解析和打印本地化字段值时,可以使用格式化程序SPI。ConversionService为两个SPI提供统一的类型转换API。

格式转换器怎么用

Springboot环境下如何自定义数据类型的转换?

Formatter SPI

Formatter SPI 实现字段格式化逻辑非常简单,而且是强类型的。以下列表显示格式化程序接口定义:

package org.springframework.format; public interface Formatter<T> extends Printer<T>, Parser<T> { }

Formatter 从Printer和Parser构建块接口扩展而来。以下列表显示了这两个接口的定义:

public interface Printer<T> { String print(T fieldValue, Locale locale); } import java.text.ParseException; public interface Parser<T> { T parse(String clientValue, Locale locale) throws ParseException; }

要创建自己的Formatter格式化程序,只需要实现上面的Formatter接口。将泛型T替换为需要格式化的对象类型 — 例如,java.util.Date。实现print()操作以打印T的实例以在客户端区域中显示。实现parse()操作,从客户端语言环境返回的格式化表示中解析T的实例。如果解析尝试失败,格式化程序应该抛出ParseException或IllegalArgumentException。注意确保格式化程序实现是线程安全的。

自定义Formatter程序

根据用户输入的信息,每个字段信息通过逗号分割,通过Formatter程序将其转换为Users对象。如输入:张三,30;将信息解析为Users对象姓名为张三,年龄为30

public class Users { private String name ; private Integer age ; }

格式化程序

public class UsersFormatter implements Formatter<Users> { @Override public String print(Users object, Locale locale) { if (Objects.isNull(object)) { return "" ; } return "【name = " + object.getName() + ", age = " + object.getAge() + "】" ; } @Override public Users parse(String text, Locale locale) throws ParseException { if (text == null || text.trim().length() == 0) { return null ; } Users user = new Users() ; // 下面做简单处理,不做校验 String[] values = text.split(",") ; user.setName(values[0]) ; user.setAge(Integer.parseInt(values[1])); return user ; } }

格式化程序定义完后,需要注册到系统中让系统能够知道如何进行转换。

@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addFormatter(new UsersFormatter()) ; } }

测试接口

@GetMapping("/save") public Object save(Users users) { return users ; }

输出:

基于注解的Formatter

可以按字段类型或注解配置字段格式。要将注解绑定到格式化程序,需要实现AnnotationFormatterFactory。以下显示了AnnotationFormatterFactory接口的定义:

package org.springframework.format; public interface AnnotationFormatterFactory<A extends Annotation> { Set<Class<?>> getFieldTypes(); Printer<?> getPrinter(A annotation, Class<?> fieldType); Parser<?> getParser(A annotation, Class<?> fieldType); }

要创建实现,请执行以下操作:

  1. 参数化要与格式逻辑关联的字段annotationType — 例如org.springframework.format.annoation.DateTimeFormat。
  2. getFieldTypes()返回可以使用注释的字段类型。
  3. getPrinter()返回Printer以打印带注释字段的值。
  4. getParser()返回一个Parser来解析带注释字段的值。
自定义注解格式化程序

自定义注解类,用来需要格式化的字段

@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) public @interface AgeFormat { }

自定义注解格式化程序

public final class AgeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<AgeFormat> { public Set<Class<?>> getFieldTypes() { Set<Class<?>> types = new HashSet<Class<?>>() ; types.add(Integer.class) ; return types; } @Override public Printer<Integer> getPrinter(AgeFormat annotation, Class<?> fieldType) { return new AgeFormatter() ; } @Override public Parser<Integer> getParser(AgeFormat annotation, Class<?> fieldType) { return new AgeFormatter() ; } private class AgeFormatter implements Formatter<Integer> { @Override public String print(Integer object, Locale locale) { if (object == null) { return "" ; } return object.toString() ; } @Override public Integer parse(String text, Locale locale) throws ParseException { if (text == null || text.trim().length() == 0) { return -1 ; } return Integer.parseInt(text.substring(1)) ; } } }

注册格式化程序

@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addFormatterForFieldAnnotation(new AgeFormatAnnotationFormatterFactory()) ; } }

Users.age字段添加注解

public class Users { private String name ; @AgeFormat private Integer age ; }

测试接口

@GetMapping("/save2") public Object save2(Users users) { return users ; }

注意这里的年龄前面加了一个‘s’字符。

注解添加到参数上

格式化程序

public final class UsersFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<UsersFormat> { public Set<Class<?>> getFieldTypes() { Set<Class<?>> types = new HashSet<Class<?>>() ; types.add(Users.class) ; return types; } @Override public Printer<?> getPrinter(UsersFormat annotation, Class<?> fieldType) { return new UsersFormatter(); } @Override public Parser<?> getParser(UsersFormat annotation, Class<?> fieldType) { return new UsersFormatter() ; } }

接口

@GetMapping("/save3") public Object save3(@UsersFormat Users users) { return users ; }

完毕!!!

给个关注+转发呗谢谢

公众号:Springboot实战案例锦集

SpringBoot多数据源配置详解

SpringBoot配置文件你了解多少?

SpringBoot RabbitMQ消息可靠发送与接收

Springboot整合RabbitMQ死信队列详解

SpringBoot整合Quartz实现任务调度

SpringBoot2 整合 OAuth2 资源认证(保护)

SpringBoot AOP 9种切入点详细使用说明

SpringBoot2 整合OAuth2实现统一认证

SpringBoot中使用Cache及JSR107的使用

SpringBoot一个提升N倍性能的操作

Springboot中接口参数校验N种方法你会几个?

Java线上CPU100% 问题排查

您可以还会对下面的文章感兴趣

使用微信扫描二维码后

点击右上角发送给好友