Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @有效的批注在spring boots中不起作用_Java_Spring Boot_Validation - Fatal编程技术网

Java @有效的批注在spring boots中不起作用

Java @有效的批注在spring boots中不起作用,java,spring-boot,validation,Java,Spring Boot,Validation,我使用SpringBoot来构建API,在post请求中,我使用有效的符号来验证作为JSON文件发布的用户输入,但是当我将一些字段留空进行测试时,它们仍然被传递。 我不知道为什么,因为我在对象参数之前使用了有效的符号 有什么不对劲 对象类 @NotNull(message = "Please provide a name") private String name; @NotNull(message = "Please provide a

我使用SpringBoot来构建API,在post请求中,我使用有效的符号来验证作为JSON文件发布的用户输入,但是当我将一些字段留空进行测试时,它们仍然被传递。 我不知道为什么,因为我在对象参数之前使用了有效的符号

有什么不对劲

对象类


    @NotNull(message = "Please provide a name")
    private String name;

    @NotNull(message = "Please provide a gender")
    private Gender gender;

    @NotNull(message = "Please provide a birthDay")
    private String birthDay;

    @NotNull(message = "Please provide a latitude")
    private double latitude;

    @NotNull(message = "Please provide a longitude")
    private double longitude;

    public Wolf(String id, @NotNull String name, @NotNull Gender gender, @NotNull String birthDay, @NotNull double latitude, @NotNull double longitude)
    {
        this.id = id!=null ? id : UUID.randomUUID().toString();
        this.name= name;
        this.gender= gender;
        this.birthDay= birthDay;
        this.latitude= latitude;
        this.longitude= longitude;
    }
rest控制器类

@Autowired
    private Wolf_Service wolf_service;

    @RequestMapping("Wolf/wolf_list")
    public List<Wolf> All_wolfs()
    {
        return wolf_service.display_wolf_list();
    }
  @PostMapping(value = "Wolf/createwolf", consumes = "application/json", produces = "application/json")
    public Wolf createwolf (@Valid @RequestBody Wolf wolf)   {
        
        var isAdded =  wolf_service.add_wolf(wolf);
        if (!isAdded) {
            return null;
        }
            return wolf;
    }
@Autowired
私人沃尔夫服务沃尔夫服务;
@请求映射(“狼/狼列表”)
公开列出所有_wolfs()
{
返回wolf_服务。显示wolf_列表();
}
@PostMapping(value=“Wolf/createwolf”,consumes=“application/json”,products=“application/json”)
public Wolf createwolf(@Valid@RequestBody-Wolf-Wolf){
var isAdded=wolf\u服务。添加wolf(wolf);
如果(!isAdded){
返回null;
}
狼归;
}

这取决于您如何处理空值。如果将字符串字段留空意味着您将发送“”,这将被传递,因为它不是空值,因此Wolf类上的@NotNull注释不适用


Michael

此问题通常出现在最新版本的spring boot(2.3.0)中

您需要添加以下依赖项:

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

org.springframework.boot
弹簧启动启动器验证

注意:您可以使用
hibernate validator
而不是
spring boot starter验证

检查是否从验证中正确导入。约束不是从com.sun.istack导入的

import javax.validation.constraints.NotNull;
如果这不能解决你的问题。 如果使用了验证依赖项,请检查pom.xml

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

org.springframework.boot
弹簧启动启动器验证

这解决了我的问题,非常感谢