@valid---用好valid,让接口干净

本文详细介绍了JSR303在实体校验中的应用,包括使用hibernate-validator进行依赖引入,以及如何通过注解如@NotEmpty、@Pattern、@Range和@NotNull等实现对字符串、数字范围、日期格式的有效校验。

第一种JSR303

在这里插入图片描述

引入

<dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
      </dependency>

实体检查

one 1,2,3 逗号隔开的单个字符串

@NotEmpty(message = "channels can't null or empty")
    @Pattern(regexp = "(?:(\\d|[1-2]\\d|3[0-2])(?:,|$))+", message = "invalid channels")
    @ApiModelProperty(value = "设备通道,1开始.数字表示通道,0表示所有通道。用英文逗号间隔,此时的通道号操作都是对所有通道号一起操作", required = true)
    private String channels;

two 不能为null

@NotNull(message = "streamType can't null")

three 数字范围

@Range(min = 1, max = 32, message = “channels must be 1~32”)

@Range(min = 1, max = 32, message = "channels must be 1~32")
    @ApiModelProperty(value = "数字表示通道,设备通道,1开始", required = true)
    private Long channel;

four 日期

先配置yml

spring:
  jackson:
    default-property-inclusion: non_null
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    deserialization:
      adjust_dates_to_context_time_zone: true

  @NotNull(message = "endTime can't be null")
//    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "结束时间, yyyy-MM-dd HH:mm:ss", required = true)
    private String endTime;
 @Pattern(regexp = "\\d{4}-\\d{2}", message = "`beginDate` must format be `yyyy-MM`")
    @ApiModelProperty(value = "查询的开始日期,格式 yyyy-MM", example = "2019-04")
    private String beginDate;
nvidia@nvidia-desktop:~$ sudo systemctl status kubelet [sudo] password for nvidia: nvidSorry, try again. [sudo] password for nvidia: Sorry, try again. [sudo] password for nvidia: ● kubelet.service - kubelet: The Kubernetes Node Agent Loaded: loaded (/lib/systemd/system/kubelet.service; enabled; vendor preset: enabled) Drop-In: /etc/systemd/system/kubelet.service.d └─10-kubeadm.conf Active: activating (auto-restart) (Result: exit-code) since Mon 2025-12-08 14:15:59 CST; 2s ago Docs: https://kubernetes.io/docs/ Process: 272923 ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_EXTRA_ARGS (code=exited, status=1/FAILURE) Main PID: 272923 (code=exited, status=1/FAILURE) nvidia@nvidia-desktop:~$ sudo systemctl start kubelet nvidia@nvidia-desktop:~$ sudo systemctl enable kubelet nvidia@nvidia-desktop:~$ sudo systemctl status kubelet ● kubelet.service - kubelet: The Kubernetes Node Agent Loaded: loaded (/lib/systemd/system/kubelet.service; enabled; vendor preset: enabled) Drop-In: /etc/systemd/system/kubelet.service.d └─10-kubeadm.conf Active: activating (auto-restart) (Result: exit-code) since Mon 2025-12-08 14:16:20 CST; 6s ago Docs: https://kubernetes.io/docs/ Main PID: 275262 (code=exited, status=1/FAILURE) Tasks: 0 (limit: 8482) Memory: 0B CGroup: /system.slice/kubelet.service nvidia@nvidia-desktop:~$ sudo systemctl status docker cri-dockerd Unit cri-dockerd.service could not be found. ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2025-12-08 14:03:36 CST; 13min ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 187514 (dockerd) Tasks: 12 Memory: 36.7M CGroup: /system.slice/docker.service └─187514 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock 12月 08 14:03:35 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:35.898617157+08:00" level=info msg="Starting up" 12月 08 14:03:35 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:35.900171330+08:00" level=info msg="detected 127.0.0.53 nameserver, assuming system> 12月 08 14:03:35 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:35.927868067+08:00" level=info msg="[graphdriver] trying configured driver: overlay> 12月 08 14:03:35 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:35.947923542+08:00" level=info msg="Loading containers: start." 12月 08 14:03:36 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:36.062852804+08:00" level=info msg="Default bridge (docker0) is assigned with an IP> 12月 08 14:03:36 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:36.111319236+08:00" level=info msg="Loading containers: done." 12月 08 14:03:36 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:36.129904507+08:00" level=info msg="Docker daemon" commit="26.1.3-0ubuntu1~20.04.1"> 12月 08 14:03:36 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:36.130052926+08:00" level=info msg="Daemon has completed initialization" 12月 08 14:03:36 nvidia-desktop dockerd[187514]: time="2025-12-08T14:03:36.170906833+08:00" level=info msg="API listen on /run/docker.sock" 12月 08 14:03:36 nvidia-desktop systemd[1]: Started Docker Application Container Engine. [1]+ Stopped sudo systemctl status docker cri-dockerd nvidia@nvidia-desktop:~$ # 强制重置节点(指定CRI socket) nvidia@nvidia-desktop:~$ sudo kubeadm reset --force --cri-socket unix:///var/run/cri-dockerd.sock [preflight] Running pre-flight checks W1208 14:16:52.493374 279014 removeetcdmember.go:106] [reset] No kubeadm config, using etcd pod spec to get data directory [reset] Deleted contents of the etcd data directory: /var/lib/etcd [reset] Stopping the kubelet service [reset] Unmounting mounted directories in "/var/lib/kubelet" [reset] Deleting contents of directories: [/etc/kubernetes/manifests /var/lib/kubelet /etc/kubernetes/pki] [reset] Deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf] The reset process does not clean CNI configuration. To do so, you must remove /etc/cni/net.d The reset process does not reset or clean up iptables rules or IPVS tables. If you wish to reset iptables, you must do so manually by using the "iptables" command. If your cluster was setup to utilize IPVS, run ipvsadm --clear (or similar) to reset your system's IPVS tables. The reset process does not clean your kubeconfig files and you must remove them manually. Please, check the contents of the $HOME/.kube/config file. nvidia@nvidia-desktop:~$ nvidia@nvidia-desktop:~$ # 清理残留文件 nvidia@nvidia-desktop:~$ sudo rm -rf /etc/kubernetes /var/lib/kubelet /var/lib/etcd nvidia@nvidia-desktop:~$ sudo rm -f /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf nvidia@nvidia-desktop:~$ # 单行执行避免格式错误 nvidia@nvidia-desktop:~$ sudo kubeadm join 192.168.255.178:6443 --token 7kaxfs.m1xlw2b4lloj20h5 --discovery-token-ca-cert-hash sha256:da23339b60f174e254da99411e5799c83e2cc2e103e65dfffececca06ca8d366 --cri-socket unix:///var/run/cri-dockerd.sock [preflight] Running pre-flight checks [WARNING Swap]: swap is enabled; production deployments should disable swap unless testing the NodeSwap feature gate of the kubelet [preflight] Reading configuration from the cluster... [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml' [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Starting the kubelet [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap... [kubelet-check] Initial timeout of 40s passed. [kubelet-check] It seems like the kubelet isn't running or healthy. [kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 127.0.0.1:10248: connect: connection refused. [kubelet-check] It seems like the kubelet isn't running or healthy. [kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 127.0.0.1:10248: connect: connection refused. [kubelet-check] It seems like the kubelet isn't running or healthy. [kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 127.0.0.1:10248: connect: connection refused. [kubelet-check] It seems like the kubelet isn't running or healthy. [kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 127.0.0.1:10248: connect: connection refused.
最新发布
12-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值