Hibernate validator 进行时间格式的自定义校验

1.使用的业务场景:

Controller层的请求方法中的参数中的时间字段需要使用某种特定的时间格式:

如我才用的时间格式为:yyyy-MM-dd'T'HH:mm:ss.SSS+HH:mm

同时使用的DTO如下所示:

package com.test.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.validation.Valid;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

@Data
@Accessors(chain = true)
public final class TestDTO {

    @NotBlank(message = "Missing Mandatory Field")
    String id;

    @NotBlank(message = "Missing Mandatory Field")
    String merchantReference;

    @NotBlank(message = "Missing Mandatory Field")
    String memberId;

    @NotBlank(message = "Missing Mandatory Field")
    String merchantId;

    @NotBlank(message = "Missing Mandatory Field")
    String type;

    @NotNull(message = "Time Format Not Allowed")
    String date;

    @Valid
    TestLocation location;

    @Valid
    TestValue value;

    @NotBlank(message = "Missing Mandatory Field")
    String data;

    @NotBlank(message = "Missing Mandatory Field")
    String dataType;

    @Valid
    TestEvent event;

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TestLocation {

        @NotBlank(message = "Missing Mandatory Field")
        String id;

        @DecimalMin(value = "0", message = "Value Not Allowed")
        @DecimalMax(value = "180", message = "Value Not Allowed")
        BigDecimal lat;

        @DecimalMin(value = "0", message = "Value Not Allowed")
        @DecimalMax(value = "90", message = "Value Not Allowed")
        BigDecimal lng;
    }

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TestValue {

        @DecimalMin(value = "0", message = "Value Not Allowed")
        BigDecimal amount;

        @NotBlank(message = "Missing Mandatory Field")
        String type;
    }

    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TestEvent {

        @NotBlank(message = "Missing Mandatory Field")
        String name;

        @Valid
        Map<String, String> data;
    }

    public void setDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+HH:mm");
        try {
            sdf.parse(date);
            this.date = date;
        } catch (ParseException e) {
            e.printStackTrace();
            this.date = null;
        }
    }
}

2.实现校验的方法:

由于没有找到相应注解和方法来对我想要的时间格式进行校验,故我采用setDate(String date)方法同@NotNull注解一起使用,

第一步:使用setDate(String date)来校验时间的格式是否为目标格式,如果不是就会产生异常。

第二步:处理相应异常后就将date字段赋值为null。

第三步:利用@NotNull注解配合自定义异常信息显示相应的异常。

异常信息显示如下,其中包括异常信息和校验异常的参数:

{
    "errorMessage": "Time Format Not Allowed",
    "parameters": [
        "date"
    ]
}
发布了22 篇原创文章 · 获赞 5 · 访问量 2199

猜你喜欢

转载自blog.csdn.net/calm_encode/article/details/103827179
今日推荐