Spring Boot白色标签错误页(类型=内部服务器错误,状态=500)

vulvrdjw  于 2022-10-04  发布在  Spring
关注(0)|答案(5)|浏览(262)

我正在尝试使用Spring Boot的在线教程,我所有的Java文件看起来都没问题。以下是它们:

User.java文件

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文件

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文件

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();       
}}

最后一个是名为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", "ramesh@gmail.com"));
this.userRepository.save(new User("Long", "Hoang", "tom@gmail.com"));
this.userRepository.save(new User("Tony", "Lan", "dir@gmail.com"));
}}

对不起,我弄错了,这是我的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>

如您所见,没有任何错误。

但是,当我将Thinghiem1Application.java文件作为Spring Boot App运行时,它告诉我有一个类似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的错误

而且页面无法加载,它对我说Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

我对这个问题很困惑,试图解决它,但仍然不能解决它,你能给我一些想法吗?提前谢谢您。

2nbm6dog

2nbm6dog1#

您需要有一个默认的构造函数,例如:

public User() {
}

Hibernate使用默认构造函数来创建实体对象。如果默认构造函数在任何实体中都不可用,则示例异常:出现意外错误(类型=内部服务器错误,状态=500)。将从休眠状态被抛出。

fcg9iug3

fcg9iug32#

在用户实体类中添加默认构造函数

9nvpjoqh

9nvpjoqh3#

您只需要在User类中添加一个空的构造函数:

PUBLIC USER(){}

sycxhyv7

sycxhyv74#

所有持久化类都必须有一个默认的构造函数,这样Hibernate才能使用Constructor.newInstance()示例化它们。建议您拥有一个至少具有包可见性的默认构造函数,以便在Hibernate中生成运行时代理。因此,您必须添加默认构造函数。

public User() { 
}
rnmwe5a2

rnmwe5a25#

Whitelabel Error Page

此应用程序没有显式的/ErrorMap,因此您将其视为一种后备。您将找到更多信息herehere

整个日志应该是这样的:

Tue Sep 27 17:27:29 WIB 2022
There was an unexpected error (type=Internal Server Error, status=500).
No message available

相关问题