英文:
Quarkus: @Valid doesn’t work with PanacheMongo
问题
我在验证我的Bean时遇到了问题。
我正在使用带有MongoDB的Quarkus,在尝试运行一个创建REST API时,在请求Bean之前使用了`@Valid`注解。我期望如果我想要创建一个具有`null`字段的文档(显然我在实体中使用了`@NotNull`),则会抛出异常,但实际上文档被创建而没有字段。
以下是我的代码:
Car.java:
```java
@MongoEntity(collection="cars")
public class Car extends PanacheMongoEntityBase {
@BsonId
private long id;
@NotNull
private String carName;
@NotNull
@Size(min = 1, max = 3)
private String code;
// 获取和设置方法
}
CarResource.java:
@Path("/cars")
@Consumes("application/json")
@Produces("application/json")
public class CarResource {
@GET
public List<Car> list() {
return Car.listAll();
}
@GET
@Path("/{id}")
public Car get(long id) {
return Car.findById(id);
}
@POST
public Response create(@Valid Car car) {
car.persist();
return Response.status(201).build();
}
我在使用@Size
注解时也遇到了相同的问题,因为我可以创建一个比3个字符更长的code
字段。
更新
使用quarkus-hibernate-validator时,验证可以正常工作。
现在,我需要为唯一字段找到解决方案。
此外,除了主要问题之外:是否有类似于@Indexed(unique = true)
的注解?我想为我的应用程序添加一个唯一字段。
<details>
<summary>英文:</summary>
I have a problem with validation of my bean.
I'm using Quarkus with MongoDB and when I try to run a create REST API, with `@Valid` annotation before request bean, I expect an exception if I want to create a document with `null` field (obviously I use `@NotNull` in entity), but document is created without field.
Here is my code:
Car.java:
```java
@MongoEntity(collection="cars")
public class Car extends PanacheMongoEntityBase {
@BsonId
private long id;
@NotNull
private String carName;
@NotNull
@Size(min = 1, max = 3)
private String code;
// get and set
}
CarResource.java:
@Path("/cars")
@Consumes("application/json")
@Produces("application/json")
public class CarResource {
@GET
public List<Car> list() {
return Car.listAll();
}
@GET
@Path("/{id}")
public Car get(long id) {
return Car.findById(id);
}
@POST
public Response create(@Valid Car car) {
car.persist();
return Response.status(201).build();
}
I have same problem with @Size
annotation, because I can create a code
field with more characters than 3.
UPDATE
Validation works with quarkus-hibernate-validator.
Now, I have to find a solution for unique field.
And besides from the main question: is there an annotation like @Indexed(unique = true)
? I want an unique field for my app.
答案1
得分: 1
@Path("/cars")
@Consumes("application/json")
@Produces("application/json")
@Validated
public class CarResource {
@GET
public List<Car> list() {
return Car.listAll();
}
@GET
@Path("/{id}")
public Car get(long id) {
return Car.findById(id);
}
@POST
public Response create(@Valid Car car) {
car.persist();
return Response.status(201).build();
}
}
英文:
You need to use @Validated
annotation on CarResource
Class as shown below.
@Path("/cars")
@Consumes("application/json")
@Produces("application/json")
@Validated
public class CarResource {
@GET
public List<Car> list() {
return Car.listAll();
}
@GET
@Path("/{id}")
public Car get(long id) {
return Car.findById(id);
}
@POST
public Response create(@Valid Car car) {
car.persist();
return Response.status(201).build();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论