【问题标题】:Bean Validation in Spring Boot Data Rest Not WorkingSpring Boot Data Rest 中的 Bean 验证不起作用
【发布时间】:2019-01-14 22:20:32
【问题描述】:

我有一个使用 Spring Data Rest 的非常简单的 Spring Boot 2.0.3 项目。

当我尝试在 http:localhost:8080/users 上使用 POST 添加具有无效电子邮件地址的用户时,会返回此 JSON:

{
    "timestamp": "2018-08-07T18:16:15.513+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Could not commit JPA transaction; nested exception is 
    javax.persistence.RollbackException: Error while committing the 
    transaction",
    "path": "/users"
}

POM.XML

<modelVersion>4.0.0</modelVersion>

<groupId>in.tschud</groupId>
<artifactId>User</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>User</name>
<description>RESTful User API</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</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>
</dependencies>

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

UserApp.java

@SpringBootApplication
public class UserApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

}

用户.java

@Data
@NoArgsConstructor
@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private long id;

    @Email
    private String email;

    private String firstName;

    private String lastName;

    private String password;

}

UserRepository.java

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends PagingAndSortingRepository<User, 
Long> {

}

我在谷歌上搜索过,几年前似乎有很多相关问题,我尝试了建议的解决方案here

但这些都不起作用。

我已经花了很多时间试图让它工作(比编写一个做同样事情的简单 JAX-RS API 端点所花费的时间要多得多),所以任何帮助都将不胜感激.

必须在下方发布控制台输出,因为此处的帖子限制为 3000 个字符。

【问题讨论】:

    标签: java spring-boot spring-data-rest bean-validation


    【解决方案1】:

    您发布的所有内容都表明它按预期工作。

    引发了 ConstraintViolationException,消息表明电子邮件确实无效。

    您可能会感到困惑的是,返回的错误默认为 Http Status 500。您一定没有在同一个控制器中声明异常处理程序,或者 @ControllerAdvice-marked 类 ex :

    @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Request content invalid")
    @ExceptionHandler(javax.validation.ConstraintViolationException.class)
    public void error() {
        // You can customize messages and extract faulty fields
    }
    

    【讨论】:

    • 感谢您的建议,但我仍然收到相同的错误,即 Spring Boot 似乎没有拾取 ConstraintViolationException。但是通过这个我找到了[这个](stackoverflow.com/questions/45070642/…)。看起来这是 Spring Boot 中的错误/功能。建议的解决方案都不起作用。我对 Spring 比较陌生,但似乎我在 Spring Boot 中触摸/尝试的所有内容都不起作用或给出了意想不到的结果,这导致我每次都要花半天时间在谷歌上搜索。到目前为止,体验不是很好。
    • 好的,我现在已经让 Bean 验证在没有 Spring Data Rest 的普通 Spring Boot 版本中按预期工作。查看更多详细信息here 我在上面的 POST UserController 中传递了 BindingResult,显然这是一个错误。但是,我仍然无法在上面的 Spring Boot Data REST 版本中使用 Bean 验证。看起来 Spring Data REST 不支持 Bean 验证,在我看来,除了在任何类型的数据存储之上的纯 API 抽象层之外,它会使其静音。
    • P.S.我刚刚意识到,我没有添加上面的 UserController,因为我后来添加了它,当我试图查看是否可以让上面的 Spring Boot Data REST Api 与自定义控制器一起工作时,我做不到。不过,关键是,您似乎不必将 BindingResult 作为参数包含在 Spring Boot 中的 POST 控制器中。
    猜你喜欢
    • 2016-03-02
    • 2022-01-09
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多