Spring Boot Data Rest中的Bean验证不起作用

时间:2018-08-07 18:33:55

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

我有一个使用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);
    }

}

User.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> {

}

我已经在Google上搜索了,几年前似乎有许多相关问题,并且我尝试了建议的解决方案here

但是这些都不起作用。

我已经花了很多时间尝试使其工作(比编写一个简单的执行相同功能的JAX-RS API端点所花的时间要多得多),所以对您的帮助将不胜感激

将不得不在下面发布控制台输出,因为此处的发布限制为3000个字符。

1 个答案:

答案 0 :(得分:0)

您发布的所有内容均显示其正常运行。

引发ConstraintViolationException,并且该消息指示该电子邮件确实是无效的。

可能使您感到困惑的是,返回的错误默认为Http Status500。您必须未在同一控制器或带有@ControllerAdvice标记的类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
}