Spring MVC:StringTrimmerEditor无法正常工作

时间:2019-07-18 08:41:44

标签: java spring jpa trim

当我传递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"
}

0 个答案:

没有答案