Spring Boot 2.0 - 验证不起作用

时间:2018-03-18 22:29:45

标签: validation spring-boot

我创建了表单,我想用Spring Tools验证。不幸的是,表单已经错误传递。我不知道是什么问题。我尝试添加到pom spring-boot-starter-validation,但它的钢铁不起作用。你能告诉我哪里有错误吗?

POM:

 <?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.xyz</groupId>
    <artifactId>library</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>library</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

控制器(保存记录的一部分):

  @RequestMapping(value = "/employee/save", method = RequestMethod.GET)
public String saveEmployee(@Valid EmployeeDTO empl, BindingResult result) {

    if (result.hasErrors()) {
        return "addEmployee";
    }
    employeeService.save(empl);

    return "redirect:/employees";
}

EmployeeDTO:

package com.xyz.dto;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

    public class EmployeeDTO {

    private Integer id = null;

    @NotNull
    @Size(max = 100)
    private String name;

    @NotNull
    @Size(max = 150)
    private String street;

    @NotNull
    @Size(max = 150)
    private String city;

@NotNull
@Email
private String email;
//getters and setters

1 个答案:

答案 0 :(得分:0)

我也有类似的问题。在更新到Spring Boot 2.0之后,验证就行不通了。问题出在方法上

@InitBinder(BasketCommandService.COMMAND_NAME)
protected final void initBinder(WebDataBinder binder) {
    binder.addValidators(validator);
}

这不能被代理。 这是构建项目时控制台输出的一部分:

2018-07-09 14:26:15.593  INFO 18600 --- [           main] o.s.aop.framework.CglibAopProxy          : Final method [protected final void cz.<companyName>.core.web.shop.orderprocess.AbstractOrderProcessController.initBinder(org.springframework.web.bind.WebDataBinder)] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.

解决方案是查看所有与上述警告类似的警告,并从受影响的方法中删除“最终”一词。