Reputation: 117
I am trying to follow the online tutorial of spring boot and all of my java file seem ok. Here they are :
User.java file
package net.javaguides.springboot.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String email;
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}}
UserRepository.java file
package net.javaguides.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.javaguides.springboot.model.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long>{
}
UserController.java file
package net.javaguides.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.javaguides.springboot.model.User;
import net.javaguides.springboot.repository.UserRepository;
@RestController
@RequestMapping("api/")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("users")
public List<User> getUsers() {
return this.userRepository.findAll();
}}
And the last one is the file named Thinghiem1Application.java
package net.javaguides.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import net.javaguides.springboot.model.User;
import net.javaguides.springboot.repository.UserRepository;
@SpringBootApplication
public class Thinghiem1Application implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(Thinghiem1Application.class, args);
}
@Autowired
private UserRepository userRepository;
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
this.userRepository.save(new User("Duc", "Nguyen", "[email protected]"));
this.userRepository.save(new User("Long", "Hoang", "[email protected]"));
this.userRepository.save(new User("Tony", "Lan", "[email protected]"));
}}
Sorry for my mistake, here is my thinghiem1/pom.xml
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>thinghiem1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>thinghiem1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
</project>
As you can see, there was no error.
But when I ran the Thinghiem1Application.java file as Spring Boot App, it said to me that there was an error like this 2021-07-15 20:28:27.457 ERROR 11092 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: No default constructor for entity: : net.javaguides.springboot.model.User; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : net.javaguides.springboot.model.User] with root cause
and the page can not load, it said to me that Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
I am very confusing with this problem and trying to fix it but still can not fix it, could you please give me some ideas ? Thank you in advance.
Upvotes: 3
Views: 12097
Reputation: 1
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback. You will find more information here or here.
The whole log should be something like:
Tue Sep 27 17:27:29 WIB 2022
There was an unexpected error (type=Internal Server Error, status=500).
No message available
Upvotes: 0
Reputation: 41
All persistent classes must have a default constructor so that Hibernate can instantiate them using Constructor.newInstance(). It is recommended that you have a default constructor with at least package visibility for runtime proxy generation in Hibernate. so you must add a default constructor.
public User() {
}
Upvotes: 0
Reputation: 2699
You just need to add an empty constructor to User class:
public User() { }
Upvotes: 0
Reputation: 306
You need to have a default constructor such as:
public User() {
}
Hibernate uses the default constructor to create entity objects. If the default constructor is not available in any of the entities, InstantiationException: There was an unexpected error (type=Internal Server Error, status=500). will be thrown from hibernate.
Upvotes: 3