package cn.bunny.config; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.InitBinder; import java.io.IOException; /** * 去除前端传递的空格 */ @ControllerAdvice public class ControllerStringParamTrimConfig { /** * 创建 String trim 编辑器 * 构造方法中 boolean 参数含义为如果是空白字符串,是否转换为null * 即如果为true,那么 " " 会被转换为 null,否者为 "" */ @InitBinder public void initBinder(WebDataBinder binder) { StringTrimmerEditor propertyEditor = new StringTrimmerEditor(false); // 为 String 类对象注册编辑器 binder.registerCustomEditor(String.class, propertyEditor); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return jacksonObjectMapperBuilder -> { // 为 String 类型自定义反序列化操作 jacksonObjectMapperBuilder .deserializerByType(String.class, new StdScalarDeserializer(String.class) { @Override public String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { // // 去除全部空格 // return StringUtils.trimAllWhitespace(jsonParser.getValueAsString()); // 仅去除前后空格 return jsonParser.getValueAsString().trim(); } }); }; } }