遇到这么个问题,前端传参数时,没值的字符串参数也会提交到后台,但是传过来的参数值并不是NULL,而是一个空字符串,所以需要解决这个问题。
1、首先创建一个配置类,实现WebMvcConfigurerAdapter:
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
//todo
...
}
2、重写WebMvcConfigurerAdapter的 addFormatters()方法:
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(String.class, new Formatter<String>() {
@Override
public String parse(String text, Locale locale) throws ParseException {
if(StringUtils.isBlank(text)){
return null;
}
return text;
}
@Override
public String print(String object, Locale locale) {
return object;
}
});
}
}