SpringBoot提交参数去除前后空格
介绍
前台提交的请求参数,自动去除前后空格
配置代码
去除路径参数的前后空格
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
@ControllerAdvice
public class GlobalControllerAdvice {
/**
* Get请求参数去除空格
* @param binder
*/
@InitBinder
public void stringTrimmerEditor(WebDataBinder binder) {
binder.registerCustomEditor(String.class,new StringTrimmerEditor(true));
}
}
去除@RequestBody json的参数前后空格
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class AppConfig {
/**
* 注解@RequestBody中JSON或XML对象参数,去除空格
* @return
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return jacksonObjectMapperBuilder -> {
// 为 String 类型自定义反序列化操作
jacksonObjectMapperBuilder
.deserializerByType(String.class, new StdScalarDeserializer<String>(String.class) {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext ctx)
throws IOException {
// 去除前后空格
return StringUtils.trim(jsonParser.getValueAsString());
}
});
};
}
}