Spring实现封装自定义注解@Trimmed清除字符串前后的空格

时间:2021-08-18 18:04:18

在Spring中实现字符串清除的方法有很多,原生方法String自带trim()方法,或者使用StringUtils提供的trim...方法。

通常可以将上面的方式封装成自定义注解的形式去实现来节省更多的业务代码。

Trimmed.java:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* 清除字符串前后的空格
*/
@Target(value = { ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Trimmed { public static enum TrimmerType {
SIMPLE, ALL_WHITESPACES, EXCEPT_LINE_BREAK;
} TrimmerType value() default TrimmerType.ALL_WHITESPACES; }

TrimmedAnnotationFormatterFactory.java:

import java.text.ParseException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern; import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Formatter;
import org.springframework.format.Parser;
import org.springframework.format.Printer; import Trimmed.TrimmerType; /**
* 清除字符串前后的空格自定义注解格式工厂
*/
public class TrimmedAnnotationFormatterFactory implements AnnotationFormatterFactory<Trimmed> { private static final Map<TrimmerType, TrimmerFormatter> TRIMMER_FORMATTER_MAP; static {
TrimmerType[] values = Trimmed.TrimmerType.values();
Map<TrimmerType, TrimmerFormatter> map = new HashMap<TrimmerType, TrimmerFormatter>(values.length);
for (TrimmerType type : values) {
map.put(type, new TrimmerFormatter(type));
}
TRIMMER_FORMATTER_MAP = Collections.unmodifiableMap(map);
} @Override
public Set<Class<?>> getFieldTypes() {
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(1, 1);
fieldTypes.add(String.class);
return fieldTypes;
} @Override
public Parser<?> getParser(Trimmed annotation, Class<?> fieldType) {
return TRIMMER_FORMATTER_MAP.get(annotation.value());
} @Override
public Printer<?> getPrinter(Trimmed annotation, Class<?> fieldType) {
return TRIMMER_FORMATTER_MAP.get(annotation.value());
} private static class TrimmerFormatter implements Formatter<String> { private static final Pattern PATTERN_WHITESPACES = Pattern.compile("\\s+");
private static final Pattern PATTERN_WHITESPACES_WITH_LINE_BREAK = Pattern.compile("\\s*\\n\\s*");
private static final Pattern PATTERN_WHITESPACES_EXCEPT_LINE_BREAK = Pattern.compile("[\\s&&[^\\n]]+"); private final TrimmerType type; public TrimmerFormatter(TrimmerType type) {
if (type == null)
throw new NullPointerException();
this.type = type;
} @Override
public String print(String object, Locale locale) {
return object;
} @Override
public String parse(String text, Locale locale) throws ParseException {
text = text.trim();
switch (type) {
case ALL_WHITESPACES:
return PATTERN_WHITESPACES.matcher(text).replaceAll(" ");
case EXCEPT_LINE_BREAK:
return PATTERN_WHITESPACES_EXCEPT_LINE_BREAK
.matcher(PATTERN_WHITESPACES_WITH_LINE_BREAK.matcher(text).replaceAll("\n")).replaceAll(" ");
case SIMPLE:
return text;
default:
// not possible
throw new AssertionError();
}
} } }

集成方法参考:http://www.cnblogs.com/EasonJim/p/7592260.html

上面注解的使用方法直接是在方法字段使用@Trimmed即可。支持属性和方法上的字段注入。

使用注意:

1、如果用了@RequestParam(defaultValue = "")时,会导致为null,但是如果defaultValue有值时,一切正常。

参考:

https://jira.spring.io/browse/SPR-7768(以上的封装来自此篇文章)

实现自定义注解的方法收集:

http://www.importnew.com/14479.html

http://blog.csdn.net/jrainbow/article/details/52382728

http://blog.csdn.net/jrainbow/article/details/46709543

http://blog.csdn.net/linhaiyun_ytdx/article/details/65939885

http://blog.csdn.net/soundfly/article/details/17380741