首页 > 解决方案 > 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"
}

标签: javaspringjpatrim

解决方案


推荐阅读