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.