`@valid`注解在Spring Boot中不起作用。

huangapple go评论83阅读模式
英文:

@valid annotation is not working in spring boots

问题

我正在使用Spring Boot构建API,在POST请求中,我使用Valid注解来验证以JSON文件形式发布的用户输入,然而,当我测试时,将一些字段留空它们仍然被传递。我不太确定为什么,因为我是在对象参数之前使用了valid注解。

对象类

@NotNull(message = "请提供姓名")
private String name;

@NotNull(message = "请提供性别")
private Gender gender;

@NotNull(message = "请提供出生日期")
private String birthDay;

@NotNull(message = "请提供纬度")
private double latitude;

@NotNull(message = "请提供经度")
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;
}
英文:

I'm using spring boot to build API, in post request I use Valid notation to validate user input that is posted as JSON file, however when I test it by leaving some fields empty they are still passed.
I'm not sure why since I'm using valid notation before the object argument.

what can be wrong?

Object class


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

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

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

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

    @NotNull(message = &quot;Please provide a longitude&quot;)
    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-controller class

@Autowired
    private Wolf_Service wolf_service;

    @RequestMapping(&quot;Wolf/wolf_list&quot;)
    public List&lt;Wolf&gt; All_wolfs()
    {
        return wolf_service.display_wolf_list();
    }
  @PostMapping(value = &quot;Wolf/createwolf&quot;, consumes = &quot;application/json&quot;, produces = &quot;application/json&quot;)
    public Wolf createwolf (@Valid @RequestBody Wolf wolf)   {
        
        var isAdded =  wolf_service.add_wolf(wolf);
        if (!isAdded) {
            return null;
        }
            return wolf;
    }

答案1

得分: 2

这个问题经常出现在最新版本的Spring Boot(2.3.0)中。

您需要添加以下依赖:

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

注意:您可以使用hibernate-validator代替spring-boot-starter-validation

英文:

This issue often comes in latest version of spring boot(2.3.0).

You would need to add the below dependency:

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;

Note : You can use hibernate-validator instead of spring-boot-starter-validation.

答案2

得分: 0

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

迈克尔

英文:

It depends on how you are handling your null values. If i.e. leaving a String field empty means you will send "" this will be passed as it is not a null value, hence the @NotNull annotation on your Wolf class does not apply.

Michael

答案3

得分: 0

请检查您是否正确地从Validation.Constraints进行导入,而不是从com.sun.istack导入。

import javax.validation.constraints.NotNull;

如果这样还没有解决您的问题,请检查您的pom.xml文件,确认是否已经添加了验证依赖。

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

Check that you import correctly from Validation.Constraints not from com.sun.istack.

import javax.validation.constraints.NotNull;

If that not fix your problem.
Check your pom.xml, if you have used validation dependency.

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年10月27日 23:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64557758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定