0

I have a very simple Spring Boot 2.0.3 project using Spring Data Rest.

When I try to add a User with an invalid email address using POST on http:localhost:8080/users, this JSON is returned:

{
    "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> {

}

I have googled and there seem to have been a number of related issues around this some years ago and I have tried the suggested solutions here

but none of these worked.

I have already spent a lot of time trying to get this to work (a lot more, than it would have taken to write a simple JAX-RS API endpoint that does the same thing), so any help would be much appreciated.

Will have to post console output below, as posts here are limited to 3000 character.

GDF
  • 157
  • 7
MET
  • 21
  • 2

1 Answers1

0

Everything you posted shows that it works as expected.

A ConstraintViolationException is raised, and the message indicates that indeed the email is invalid.

What might confuse you is that the error returned is defaulted to Http Status 500. You must not have declared an exception handler in the same controller, or a @ControllerAdvice-marked class 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
}
GDF
  • 157
  • 7
  • Thanks for the suggestion, but I still get the same error, i.e. Spring Boot does not seem to pick-up the ConstraintViolationException. But through this I found [this] (https://stackoverflow.com/questions/45070642/springboot-doesnt-handle-javax-validation-constraintviolationexception). Looks like this is a bug/ feature in Spring Boot. None of the suggested solutions work. I am relatively new to Spring, but it seems that everything I touch/ try in Spring Boot does not work or gives unexpected results, which cause me spending half a day of googling each time. Not a great experience so far. – MET Aug 07 '18 at 23:02
  • Ok, I have now got Bean validation to work as expected in a plain Spring Boot Version without Spring Data Rest. See more details [here](https://github.com/spring-projects/spring-boot/issues/14027#issuecomment-411763719) I passed the BindingResult in the above POST UserController and apparently that is a mistake. However, I still cannot use Bean validation in a Spring Boot Data REST version as above. It looks like Spring Data REST does not support Bean validation, which to my mind kind of renders it mute for anything else than a pure API abstraction layer on top of any kind of data store. – MET Aug 09 '18 at 14:00
  • P.S. I just realised, that I did not add the UserController above, as I added it afterwards, when I tried to see whether I could get the above Spring Boot Data REST Api to work with a custom controller, which I could not. Still, the point is, that you do not seem to have to include the BindingResult as a parameter to POST controllers in Spring Boot. – MET Aug 09 '18 at 14:05