java - Spring MVC:StringTrimmerEditor 不工作
问题描述
当我传递 JSON 以在字符串字段中保存带有空格的数据时,这些空格不会被修剪。真的很想了解我哪里出错了。
我曾尝试使用调试器检查 initbinder 是否在映射方法之前实际运行,并发现它工作正常。
Controller class:
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}
@PostMapping("/new")
public Discount createDiscount(@Valid @RequestBody Discount discount, BindingResult bindingResult, @RequestParam("userId") String userId) {
log.debug("Creating new discount with discount code: " + discount.getDiscountCode());
if(bindingResult.hasErrors()) {
throw new EntityIsRequiredException(bindingResult.getAllErrors().get(0).getDefaultMessage());
}
return discountService.createDiscount(discount, userId);
}
Entity class:
@Document(collection = "discount")
@Getter @Setter @ToString
public class Discount {
@Id
public String id;
@Indexed(unique = true)
@Field(value = "discount_code")
@NotNull(message = "Discount code cannot be blank")
@Size(min = 1, message = "Discount code cannot be blank")
private String discountCode;
@Field(value = "discount_percentage")
@NotNull(message = "Discount amount cannot be blank")
@Min(value = 0, message = "Discount percentage cannot be less than 0")
@Max(value = 100, message = "Discount percentage cannot be greater than 100")
private Integer discountPercentage;
@Field(value = "net_discount")
@NotNull(message = "Net discount cannot be blank")
@Min(value = 0, message = "Net discount cannot be less than 0")
private Double netDiscount;
@Field(value = "is_active")
@NotNull(message = "Status cannot be blank")
private Boolean isActive;
@Field(value = "created_by")
private String createdBy;
@Field(value = "created_on")
private LocalDateTime createdOn;
@Field(value = "updated_by")
private String updatedBy;
@Field(value = "updated_on")
private LocalDateTime updatedOn;
@Field(value = "applied_count")
private Integer appliedCount;
@Field(value = "subscription_id")
private String subscriptionId;
@Field(value = "is_used")
private Boolean isUsed;
@Field(value = "used_by")
private String usedBy;
}
Input JSON:
{
"discountCode" : " ",
"discountPercentage": 90,
"netDiscount": 2,
"isActive": false,
"appliedCount": 22,
"subscriptionId": " 3csaiofn490354094r0jkdb",
"isUsed": false,
"usedBy": "8cbfeifeh6dwvuhvj"
}
Output JSON:
{
"id": "5d303087f82c383b43f028f3",
"discountCode": " ",
"discountPercentage": 90,
"netDiscount": 2,
"isActive": false,
"createdBy": "2cdvhejbbiudihiq342kffbbf",
"createdOn": "2019-07-18T14:10:39.535",
"updatedBy": null,
"updatedOn": null,
"appliedCount": 22,
"subscriptionId": " 3csaiofn490354094r0jkdb",
"isUsed": false,
"usedBy": "8cbfeifeh6dwvuhvj"
}
解决方案
推荐阅读
- javascript - 从内容中删除特定标签并保留简码
- excel - 筛选筛选条件,然后在 Excel 中的 countif 语句中应用
- python - Pythonista 核心模块
- php - TCPDF中文字体检测
- c - 在终端(C 文件)中运行程序并且未运行到完成
- angular - Angular 项目的 idpiframe_initialization_failed
- javascript - TypeError: axios.get 不是函数?
- android - react-native-firebase - 收到推送通知后应用程序崩溃(Android)
- sql-server - SQL Server - 使用 ROWS BETWEEN UNBOUNDED PRECEDING 的分区过度不起作用?
- c# - C# 共享项目的最佳方式,但只公开所需的方法