springboot 自定义格式化器(Formatter)


springboot 自定义格式化器(Formatter)

 

应用:自定义格式化注解解析字符串

 

 

***********************

相关类与接口

 

Formatter

public interface Formatter<T> extends Printer<T>, Parser<T> {

}

 

Parser:解析指定格式的字符串,转换为对象T

@FunctionalInterface
public interface Parser<T> {

	T parse(String text, Locale locale) throws ParseException;

}

 

Printer:转换后的对象T输出为字符串

@FunctionalInterface
public interface Printer<T> {

	String print(T object, Locale locale);

}

 

AnnotationFormatterFactory

public interface AnnotationFormatterFactory<A extends Annotation> {

	Set<Class<?>> getFieldTypes();  //注解可以标注的字段类型

	Printer<?> getPrinter(A annotation, Class<?> fieldType);
	Parser<?> getParser(A annotation, Class<?> fieldType);

}

 

 

***********************

示例

 

*****************

formatter 层

 

BooleanFormat

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

}

 

BooleanFormatter

@Component
public class BooleanFormatter implements Formatter<Boolean> {

    private final String[] values={"是","1","ok","true"};

    @Override
    public Boolean parse(String text, Locale locale) throws ParseException {
        return Arrays.asList(values).contains(text.toLowerCase());
    }

    @Override
    public String print(Boolean object, Locale locale) {
        return object?"true":"false";
    }
}

 

CustomBooleanFormatAnnotationFactory

@Component
public class CustomBooleanFormatAnnotationFactory implements AnnotationFormatterFactory<BooleanFormat> {

    @Resource
    private BooleanFormatter booleanFormatter;

    @Override
    public Set<Class<?>> getFieldTypes() {
        Set<Class<?>> set=new HashSet<>();
        set.add(Boolean.class);

        return set;
    }

    @Override
    public Parser<?> getParser(BooleanFormat annotation, Class<?> fieldType) {
        return booleanFormatter;
    }

    @Override
    public Printer<?> getPrinter(BooleanFormat annotation, Class<?> fieldType) {
        return booleanFormatter;
    }
}

 

*****************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(@BooleanFormat Boolean value){
        System.out.println(value);

        return value.toString();
    }
}

 

 

***********************

使用测试

 

localhost:8080/hello?value=ok

2020-07-12 10:56:50.812  INFO 5376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-07-12 10:56:50.823  INFO 5376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 11 ms
true

 

localhost:8080/hello?value=是

true

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值