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;
}
}