夸克斯(Quarkus):@Valid 与 PanacheMongo 不兼容。

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

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&#39;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=&quot;cars&quot;)
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(&quot;/cars&quot;)
@Consumes(&quot;application/json&quot;)
@Produces(&quot;application/json&quot;)
public class CarResource {

    @GET
    public List&lt;Car&gt; list() {
        return Car.listAll();
    }

    @GET
    @Path(&quot;/{id}&quot;)
    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(&quot;/cars&quot;)
@Consumes(&quot;application/json&quot;)
@Produces(&quot;application/json&quot;)
@Validated
public class CarResource {

    @GET
    public List&lt;Car&gt; list() {
        return Car.listAll();
    }

    @GET
    @Path(&quot;/{id}&quot;)
    public Car get(long id) {
        return Car.findById(id);
    }

    @POST
    public Response create(@Valid Car car) {
        car.persist();
        return Response.status(201).build();
    }
}

huangapple
  • 本文由 发表于 2020年4月11日 07:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61150168.html
匿名

发表评论

匿名网友

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

确定