hibernate-validator校验对象属性为List

简介: hibernate-validator校验对象属性为List

君子拙于不知己而信于知己也——司马迁

文档:

https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#_with_list

我们这里首先引入starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>


然后我们带两个Entity以及一个Controller

package com.ruben.simplestreamquery.pojo;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
@Data
public class TestEntity {
    @NotNull
    private Long id;
    @NotEmpty
    private List<EntityItem> list;
}
package com.ruben.simplestreamquery.pojo;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
 * @author VampireAchao
 * @since 2022/12/20 14:57
 */
@Data
public class EntityItem {
    @NotNull
    private Long id;
}
package com.ruben.simplestreamquery.controller;
import com.ruben.simplestreamquery.pojo.TestEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * TestController
 *
 * @author VampireAchao
 * @since 2022/10/5
 */
@Slf4j
@RestController
public class TestController {
    @PostMapping("test")
    public void list(@RequestBody @Validated TestEntity testEntity) {
        log.info("请求成功!{}", testEntity);
    }
}

我们编写一个Mock

package com.ruben.simplestreamquery;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruben.simplestreamquery.pojo.EntityItem;
import com.ruben.simplestreamquery.pojo.TestEntity;
import io.github.vampireachao.stream.core.collection.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
 * @author VampireAchao
 * @since 2022/12/20 13:48
 */
@SpringBootTest
@AutoConfigureMockMvc
class TestControllerTest {
    @Test
    void test(@Autowired MockMvc mockMvc, @Autowired ObjectMapper objectMapper) throws Exception {
        final TestEntity entity = new TestEntity();
        entity.setId(1L);
        entity.setList(Lists.of(new EntityItem()));
        final String jsonStr = objectMapper.writeValueAsString(entity);
        mockMvc.perform(post("/test")
                        .contentType("application/json")
                        .content(jsonStr))
                .andExpect(status().isOk());
    }
}

此时我们对List的校验失效了

如何生效?

List加上@Valid注解

package com.ruben.simplestreamquery.pojo;
import lombok.Data;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
@Data
public class TestEntity {
    @NotNull
    private Long id;
    @NotEmpty
    private List<@Valid EntityItem> list;
}

再次测试

校验生效了

我们稍微封装一下异常处理

package com.ruben.simplestreamquery.handler;
import com.ruben.simplestreamquery.pojo.vo.GlobalResult;
import io.github.vampireachao.stream.core.stream.Steam;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
/**
 * ResponseHandler
 *
 * @author VampireAchao
 * @since 2022/10/5
 */
@RestControllerAdvice
public class ResponseHandler {
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public GlobalResult parameterValidatorResolver(MethodArgumentNotValidException e) {
        List<FieldError> errors = e.getBindingResult().getFieldErrors();
        final GlobalResult result = GlobalResult.error();
        result.set("msg", Steam.of(errors).map(error -> error.getField() + " " + error.getDefaultMessage()).join("  "));
        return result;
    }
}

然后打印出来响应结果

package com.ruben.simplestreamquery;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruben.simplestreamquery.pojo.EntityItem;
import com.ruben.simplestreamquery.pojo.TestEntity;
import io.github.vampireachao.stream.core.collection.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
 * @author VampireAchao
 * @since 2022/12/20 13:48
 */
@Slf4j
@SpringBootTest
@AutoConfigureMockMvc
class TestControllerTest {
    @Test
    void test(@Autowired MockMvc mockMvc, @Autowired ObjectMapper objectMapper) throws Exception {
        final TestEntity entity = new TestEntity();
        entity.setId(1L);
        entity.setList(Lists.of(new EntityItem()));
        final String jsonStr = objectMapper.writeValueAsString(entity);
        final MvcResult result = mockMvc.perform(post("/test")
                        .contentType("application/json")
                        .content(jsonStr))
                .andExpect(status().isOk())
                .andReturn();
        log.info(result.getResponse().getContentAsString());
    }
}

成功提示

相关文章
|
6月前
|
Java 数据库连接
后端校验(hibernate-validator)
后端校验(hibernate-validator)
160 0
|
6月前
|
Java
java8中List对象转另一个List对象
java8中List对象转另一个List对象
277 0
|
6月前
|
数据处理
利用Stream流将取到的对象List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>形式
利用Stream流将取到的对象List<对象>形式数据进行分组统计转变成Map<分组条件,数量统计>形式
60 0
|
1月前
|
JSON 前端开发 JavaScript
json字符串如何转为list对象?
json字符串如何转为list对象?
218 7
|
3月前
|
TensorFlow 算法框架/工具 Python
从numpy,list对象创建
【8月更文挑战第12天】从numpy,list对象创建。
26 8
|
3月前
|
SQL 关系型数据库 MySQL
INSERT INTO t_a.tableName SELECT * FROM t_b.tableName 如何通过定义一个list对象,包含多个tableName,循环执行前面的sql,用MySQL的语法写
【8月更文挑战第7天】INSERT INTO t_a.tableName SELECT * FROM t_b.tableName 如何通过定义一个list对象,包含多个tableName,循环执行前面的sql,用MySQL的语法写
41 5
|
5月前
|
前端开发 开发者
CSS列表属性:list-style系列属性详解
CSS列表属性:list-style系列属性详解
260 40
|
3月前
|
存储 SQL Java
|
5月前
|
Java
Java list中的对象转为list,list中的对象转为map
Java list中的对象转为list,list中的对象转为map
108 1
|
6月前
使用Hibernate-Validate进行参数校验
使用Hibernate-Validate进行参数校验
75 3