在springboot中去除get请求和post请求中的空格

  1. controller
package com.example.demo.web;

import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.*;

/**
 * description
 * @author xichengxml
 * @date 2019-12-03 21:06:26
 */
@RestController
public class StringTrimController {

	@GetMapping("/get")
	public String getTest(String id, String name) {
		return id + "--" + name;
	}


	@PostMapping("post")
	public String postTest(@RequestBody User user) {
		return user.toString();
	}
	
	static class User {
		@Setter
		@Getter
		private String id;
		@Setter
		@Getter
		private String name;

		@Override
		public String toString() {
			return "User{" +
					"id='" + id + '\'' +
					", name='" + name + '\'' +
					'}';
		}
	}
}

  1. advice
package com.example.demo.aop;

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.http.converter.json.Jackson2ObjectMapperBuilder;
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 java.io.IOException;

/**
 * description
 *
 * @author xichengxml
 * @date 2019-12-03 21:10:11
 */
@ControllerAdvice(basePackages = {"com.example.demo.web"})
public class GlobalAdvice {

	/**
	 * 去除get方式的参数空格
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 创建 String trim 编辑器
		// 构造方法中 boolean 参数含义为如果是空白字符串,是否转换为null
		// 即如果为true,那么 " " 会被转换为 null,否者为 ""
		StringTrimmerEditor propertyEditor = new StringTrimmerEditor(false);
		// 为 String 类对象注册编辑器
		binder.registerCustomEditor(String.class, propertyEditor);
	}

	/**
	 * 去除post requestBody实体中的空格
	 * @return
	 */
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		return new Jackson2ObjectMapperBuilderCustomizer() {
			@Override
			public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
				jacksonObjectMapperBuilder
						.deserializerByType(String.class, new StdScalarDeserializer<Object>(String.class) {
							@Override
							public String deserialize(JsonParser jsonParser, DeserializationContext ctx)
									throws IOException {
								return StringUtils.trimWhitespace(jsonParser.getValueAsString());
							}
						});
			}
		};
	}
}
  1. 测试用例
    get:http://localhost:8080/get?id=%20%20%201%20%20%20&name=%20%20%20222
    post:
{
    "id": "   1",
    "name": "   222"
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值