springboot 自定義 formatter 註解

咱們在開發時會用到 @DateTimeFormat 這個註解。spring

對於從前臺接收時間日期格式 很方便。springboot

但若是前臺傳來的是 "是" 「否」 「有」 "無" 這樣的中文時,想要轉成boolean 類型時,沒有對應的註解,下面咱們本身來實現這個註解。ide

本例基於this

springboot 2.xspa

jdk1.8code

首先,建立一個註解orm

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
public @interface BooleanFormat {
    String[] trueTag() default {};
}

其中 trueTag  用於接收參數blog

好比咱們想將 「YES」,「OK」 這樣的字符串轉成 true ,那須要在參數中進行表示 。開發

而後,咱們建一個formatter類,幫助咱們來實現具體的轉換規則。字符串

 1 public class BooleanFormatter implements Formatter<Boolean> {
 2 
 3     private String[] trueTag;
 4 
 5 
 6     @Override
 7     public Boolean parse(String s, Locale locale) throws ParseException {
 8         if (trueTag != null && trueTag.length > 0) {
 9             return Arrays.asList(trueTag).contains(s);
10         } else {
11             switch (s.toLowerCase()) {
12                 case "true":
13                 case "1":
14                 case "是":
15                 case "有":
16                     return true;
17             }
18         }
19         return false;
20     }
21 
22     @Override
23     public String print(Boolean aBoolean, Locale locale) {
24         return aBoolean ? "true" : "false";
25     }
26 
27     public String[] getTrueTag() {
28         return trueTag;
29     }
30 
31     public void setTrueTag(String[] trueTag) {
32         this.trueTag = trueTag;
33     }
34 }

第3行的屬性用來接收註解上傳過來的參數。

第7行的方法是用於將字符串轉成BOOLEAN類型。

第23行的方法用於將BOOLEAN類型轉成字符串。

 

完成上面的代碼後,咱們還須要將咱們自定義的類型轉類註冊到spring中。

先定義一下factory類。

 1 public class BooleanFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
 2         implements AnnotationFormatterFactory<BooleanFormat> {
 3 
 4 
 5     @Override
 6     public Set<Class<?>> getFieldTypes() {
 7         return new HashSet<Class<?>>(){{
 8             add(String.class);
 9             add(Boolean.class);
10         }};
11 
12     }
13 
14     @Override
15     public Printer<?> getPrinter(BooleanFormat booleanFormat, Class<?> aClass) {
16         BooleanFormatter booleanFormatter = new BooleanFormatter();
17         booleanFormatter.setTrueTag(booleanFormat.trueTag());
18         return booleanFormatter;
19     }
20 
21     @Override
22     public Parser<?> getParser(BooleanFormat booleanFormat, Class<?> aClass) {
23         BooleanFormatter booleanFormatter = new BooleanFormatter();
24         booleanFormatter.setTrueTag(booleanFormat.trueTag());
25         return booleanFormatter;
26     }
27 }

 

 

而後將這個類註冊到spring中。

 1 @Configuration
 2 public class WebConfigurer implements WebMvcConfigurer {
 3 
 4     
 5 
 6     @Override
 7     public void addFormatters(FormatterRegistry registry) {
 8         registry.addFormatterForFieldAnnotation(new BooleanFormatAnnotationFormatterFactory());
 9     }
10 
11     
12 }

接下來,就能夠使用這個註解了。

在你的pojo類中:

1 @Data
2 public class DemoDto {
3     @BooleanFormat(trueTag = {"YES","OK","是"})
4     private boolean exists;
5 }
相關文章
相關標籤/搜索