import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CommonConfig implements WebMvcConfigurer {
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new Converter<String, String[]>() {
@Override
public String[] convert(String source) {
String[] strings = null;
if (source != null) {
strings = new String[] { source };
}
return strings;
}
});
registry.addConverter(new Converter<String[], String>() {
@Override
public String convert(String[] values) {
String str = null;
if (values != null) {
str = values.length > 0 ? values[0] : null;
}
return str;
}
});
registry.addConverter(new Converter<Object[], Object>() {
@Override
public Object convert(Object[] values) {
Object str = null;
if(isNum((String) (values)[0])) {
str = ((Object[]) values)[0];
}else {
str = "0";
}
return str;
}
});
}
private boolean isNum(String str) {
try {
Long.parseLong(str);
} catch (Exception e) {
return false;
}
return true;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56