Spring Boot Vaadin验证不起作用-为什么? [英] Spring Boot Vaadin Validation does not work - Why?

查看:124
本文介绍了Spring Boot Vaadin验证不起作用-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Vaadin 14和Vaadin CRUD插件,看来我的验证对我不起作用.

如果我打开CRUD视图并查看这两个字段.它们是空字符串,例如null.我想收到一个错误必须不为空".但是在这里,我完全没有错误.

我的实体看起来像这样.

  @Entity(name ="Data")@数据公开课数据{@ID@GeneratedValue(策略= GenerationType.IDENTITY)私人长号;@NotNull私有字符串OrderNumber;@NotNull私有字符串序列号; 

如果我尝试保存到数据库中,它看起来像这样.

 原因:javax.validation.ConstraintViolationException:在组[javax.validation.groups.Default,]的更新期间,对类[se.danielmartensson.entities.LX_Data]的验证失败.违反约束的清单:[ConstraintViolationImpl {interpolatedMessage ='一定不能为null',propertyPath = SerialNumber,rootBeanClass = class se.danielmartensson.entities.Data,messageTemplate ='{javax.validation.constraints.NotNull.message}'}ConstraintViolationImpl {interpolatedMessage ='一定不能为null',propertyPath = OrderNumber,rootBeanClass = class se.danielmartensson.entities.Data,messageTemplate ='{javax.validation.constraints.NotNull.message}'}] 

这很奇怪,因为我的 pom.xml 包含了 Spring Boot Starter Validation .Vaadin自己说:应该包括以下内容:

请访问此Web应用程序,并尝试按上方的笔来更改CRUD.尝试使用干净的文本字段进行更新.

https://alejandro.app.fi/crud-ui-demo/simple

他的实体看起来像这样.

 /*** @作者亚历杭德罗·杜阿尔特*/@实体公共类用户{@NotNull@ID@GeneratedValue私人Long ID;@NotNull私有字符串名称; 

https://github.com/alejandro-du/crudui/blob/master/demo/src/main/java/org/vaadin/crudui/demo/entity/User.java

是的,我正在使用

  crud.getCrudFormFactory().setUseBeanValidation(true); 

这里:

 //网格的配置GridCrud< Data>crud =新的GridCrud(Data.class);crud.getGrid().setColumns("orderNumber","serialNumber");crud.getGrid().setColumnReorderingAllowed(true);//筛选TextField orderNumberFilter = createFilterField("Order Number",crud);TextField serialNumberFilter = createFilterField("Serial Number",crud);crud.getCrudLayout().addFilterComponents(orderNumberFilter,serialNumberFilter);//用户界面的配置crud.getCrudFormFactory().setUseBeanValidation(true);crud.getCrudFormFactory().setVisibleProperties("orderNumber","serialNumber");crud.getCrudFormFactory().setDisabledProperties("id"); 

解决方案

似乎基于您的设置者和获取者,它找到了 orderNumber serialNumber 属性./p>

但是,您将字段命名为 OrderNumber SerialNumber ,这不是标准的命名约定.因此,它不会在这些注释和属性之间建立联系.

您可以执行以下两项操作之一:

  1. 将字段重命名为 orderNumber serialNumber .
  2. 将注释移至getter方法.看来您正在使用Lombok,因此您必须先定义这些吸气剂,或者在字段上使用 @Getter(onMethod _ = @ NotNull).

I'm using Vaadin 14 with Vaadin CRUD AddOns and it seems that my validation does not work for me.

If I open my CRUD view and look at these two fields. They are empty strings e.g null. I'm suppose to recieve an error "must not be null". But here, I get no error at all.

My entity looks like this.

@Entity(name = "Data")
@Data
public class Data {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @NotNull
    private String OrderNumber;
    @NotNull
    private String SerialNumber;

And if I trying to save to my database, it looks like this.

Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [se.danielmartensson.entities.LX_Data] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=SerialNumber, rootBeanClass=class se.danielmartensson.entities.Data, messageTemplate='{javax.validation.constraints.NotNull.message}'}
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=OrderNumber, rootBeanClass=class se.danielmartensson.entities.Data, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]

That is weird because my pom.xml has Spring Boot Starter Validation included. Something Vaadin him self says it should be included: https://vaadin.com/learn/tutorials/introduction-to-java-bean-validation

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.application</groupId>
    <artifactId>test-data</artifactId>
    <name>Project base for Spring Boot and Vaadin Flow</name>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>11</java.version>
        <vaadin.version>14.5.2</vaadin.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
    </parent>

    <repositories>
        <!-- The order of definitions matters. Explicitly defining central here to make sure it has the highest priority. -->

        <!-- Main Maven repository -->
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <!-- Repository used by many Vaadin add-ons -->
        <repository>
            <id>Vaadin Directory</id>
            <url>https://maven.vaadin.com/vaadin-addons</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <!-- Main Maven repository -->
        <pluginRepository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <!-- Replace artifactId with vaadin-core to use only free components -->
            <artifactId>vaadin</artifactId>
            <exclusions>
                <!-- Webjars are only needed when running in Vaadin 13 compatibility mode -->
                <exclusion>
                    <groupId>com.vaadin.webjar</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.webjars.bowergithub.insites</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.webjars.bowergithub.polymer</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.webjars.bowergithub.polymerelements</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.webjars.bowergithub.vaadin</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.webjars.bowergithub.webcomponents</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
            <exclusions>
                <!-- Excluding so that webjars are not included. -->
                <exclusion>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.vaadin.artur</groupId>
            <artifactId>a-vaadin-helper</artifactId>
            <version>1.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <!-- FTP Client -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version>
        </dependency>

        <!-- Getter & Setter -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- Data base -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <!--  
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency> -->
        
        <!-- CRUD -->
        <dependency>
            <groupId>org.vaadin.crudui</groupId>
            <artifactId>crudui</artifactId>
            <version>4.3.2</version>
        </dependency>
        
        <!-- Charts -->
        <dependency>
           <groupId>com.github.appreciated</groupId>
           <artifactId>apexcharts</artifactId>
           <version>2.0.0.beta11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-testbench</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Include JUnit 4 support for TestBench and others -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- Clean build and startup time for Vaadin apps sometimes may exceed
                     the default Spring Boot's 30sec timeout.  -->
                <configuration>
                    <wait>500</wait>
                    <maxAttempts>240</maxAttempts>
                </configuration>
            </plugin>

            <!--
                Take care of synchronizing java dependencies and imports in
                package.json and main.js files.
                It also creates webpack.config.js if not exists yet.
            -->
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-frontend</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- Production mode is activated using -Pproduction -->
            <id>production</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.vaadin</groupId>
                        <artifactId>vaadin-maven-plugin</artifactId>
                        <version>${vaadin.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-frontend</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <productionMode>true</productionMode>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>it</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>start-spring-boot</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>stop-spring-boot</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Runs the integration tests (*IT) after the server is started -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <trimStackTrace>false</trimStackTrace>
                            <enableAssertions>true</enableAssertions>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

    </profiles>
</project>

I'm expecting a behaviour like this.

Please, see this web application and try to change the CRUD by pressing at the pen above. Try to update with a clean text field.

https://alejandro.app.fi/crud-ui-demo/simple

And his Entity looks like this.

/**
 * @author Alejandro Duarte
 */
@Entity
public class User {

    @NotNull
    @Id
    @GeneratedValue
    private Long id;

    @NotNull
    private String name;

https://github.com/alejandro-du/crudui/blob/master/demo/src/main/java/org/vaadin/crudui/demo/entity/User.java

And yes, I'm using

crud.getCrudFormFactory().setUseBeanValidation(true);

Here:

// Configuration of the grid
GridCrud<Data> crud = new GridCrud<>(Data.class);
crud.getGrid().setColumns("orderNumber", "serialNumber");
crud.getGrid().setColumnReorderingAllowed(true);

// Filter
TextField orderNumberFilter = createFilterField("Order Number", crud);
TextField serialNumberFilter = createFilterField("Serial Number", crud);
crud.getCrudLayout().addFilterComponents(orderNumberFilter, serialNumberFilter);

// Configuration of the user interface
crud.getCrudFormFactory().setUseBeanValidation(true);
crud.getCrudFormFactory().setVisibleProperties("orderNumber", "serialNumber");
crud.getCrudFormFactory().setDisabledProperties("id");

解决方案

It seems that based on your setters and getters, it finds the orderNumber and serialNumber properties.

However, you name your fields OrderNumber and SerialNumber, which is not the standard naming convention. Because of this, it does not make the connection between those annotations and properties.

You can do one of two things:

  1. Rename your fields to orderNumber and serialNumber.
  2. Move the annotations to the getter methods. It looks like you're using Lombok, so you'd have to define those getters first, or perhaps using @Getter(onMethod_=@NotNull) on your field.

这篇关于Spring Boot Vaadin验证不起作用-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆