【SpringBoot web-1】web项目数据校验(1)

使用 Hibernate Validator 校验数据,需要定义一个接收的数据模型,使用注解的形式描述字段校验的规则,我们以 Student 对象为例为大家演示如何使用。

首先在 WebController 添加一个保存的方法 saveStudent,参数为 Student。


@RequestMapping("/saveStudent")

public void saveUser(@Valid Student student,BindingResult result) {

    System.out.println("student:" + student);

    if(result.hasErrors()) {

        List<ObjectError> list = result.getAllErrors();

        for (ObjectError error : list) {

            System.out.println(error.getCode()+ "-" + error.getDefaultMessage());

        }

    }

}



  1. @Valid 参数前面添加 @Valid 注解,代表此对象使用了参数校验;

  2. BindingResult参数校验的结果会存储在此对象中,可以根据属性判断是否校验通过,校验不通过可以将错误信息打印出来。

接下来在 User 中给需要校验的参数添加对应的注解,对不同的属性,按照规则添加不同的校验内容。


public class Student {

    @NotEmpty(message="姓名不能为空")

    private String name;

    @Max(value = 100, message = "年龄不能大于100岁")

    @Min(value= 18 ,message= "必须年满18岁!" )

    private int age;

    @NotEmpty(message="密码不能为空")

    @Length(min=6,message="密码长度不能小于6位")

    private String password;

    //...

}



其中,message=“密码不能为空”,为自定义返回的错误信息。

使用 MockMvc 进行一个测试:


@Test

public void saveStudents() throws Exception {

    mockMvc.perform(MockMvcRequestBuilders.post("/saveStudent")

            .param("name","")

            .param("age","101")

            .param("password","test")

    );

}



结果返回:


user:name=,age=666,pass=test



# Kafka实战笔记

> **关于这份笔记,为了不影响大家的阅读体验,我只能在文章中展示部分的章节内容和核心截图**

![image.png](https://img-blog.csdnimg.cn/img_convert/17329f527e50fab0d4339760aaac7a7a.webp?x-oss-process=image/format,png)


*   **Kafka入门**
*   **为什么选择Kafka**
*   **Karka的安装、管理和配置**

![image.png](https://img-blog.csdnimg.cn/img_convert/fbe838779948422beed852af9f33bedb.webp?x-oss-process=image/format,png)


*   **Kafka的集群**
*   **第一个Kafka程序**
*   ![image.png](https://img-blog.csdnimg.cn/img_convert/55a773f7472e015ac2f980ea1bb62ce8.webp?x-oss-process=image/format,png)


afka的生产者

![image.png](https://img-blog.csdnimg.cn/img_convert/c36bc1a2fc00b7001849d72062fb9895.webp?x-oss-process=image/format,png)

*   **Kafka的消费者**
*   **深入理解Kafka**
*   **可靠的数据传递**

![image.png](https://img-blog.csdnimg.cn/img_convert/19b6082224347ff265714f1105263ff0.webp?x-oss-process=image/format,png)


![image.png](https://img-blog.csdnimg.cn/img_convert/1edf5ee6958bb40291845cf59d07f909.webp?x-oss-process=image/format,png)


*   **Spring和Kalka的整合**
*   **Sprinboot和Kafka的整合**
*   **Kafka实战之削峰填谷**
*   **数据管道和流式处理(了解即可)**

![image.png](https://img-blog.csdnimg.cn/img_convert/64b00efe01b6c486f5ef088692ca4253.webp?x-oss-process=image/format,png)


*   **Kafka实战之削峰填谷**

![image.png](https://img-blog.csdnimg.cn/img_convert/c7176fcd7cd368f9a2204907c7b9f068.webp?x-oss-process=image/format,png)

   **Kafka实战之削峰填谷**
*   **数据管道和流式处理(了解即可)**

[外链图片转存中...(img-iuNr8D8R-1714520671384)]


*   **Kafka实战之削峰填谷**

[外链图片转存中...(img-M1jgrfI3-1714520671384)]

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/topics/618154847)收录**
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值