英文:
@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 = "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-controller class
@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;
}
答案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:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论