|
|
@@ -0,0 +1,44 @@
|
|
|
+package com.ydtech.config;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+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.AutoConfigureAfter;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
|
|
+import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
|
|
+import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.WebDataBinder;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.InitBinder;
|
|
|
+import org.springframework.web.servlet.DispatcherServlet;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
+
|
|
|
+import javax.servlet.Servlet;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
|
|
+@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
|
|
|
+@AutoConfigureAfter(WebMvcAutoConfiguration.class)
|
|
|
+public class WebMvcStringTrimAutoConfiguration {
|
|
|
+
|
|
|
+ @ControllerAdvice
|
|
|
+ public static class ControllerStringParamTrimConfig {
|
|
|
+
|
|
|
+ @InitBinder
|
|
|
+ public void initBinder(WebDataBinder binder) {
|
|
|
+ // 参数:去除两端空格后 空串是否转换为 null true:null false:""
|
|
|
+ StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
|
|
|
+ // 为 String 类对象注册编辑器
|
|
|
+ binder.registerCustomEditor(String.class, stringTrimmerEditor);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|