gpt4 book ai didi

java - Spring启动问题 "There was an unexpected error (type=Internal Server Error, status=500)."

转载 作者:行者123 更新时间:2023-11-30 01:44:04 25 4
gpt4 key购买 nike

我用 spring-boot 启动了一个项目,实际上我正在尝试在本地主机端口上获取 JSON(如果我理解的话)信息。但我遇到了这个错误:“内部服务器错误,状态= 500”

我在某个地方犯了一个愚蠢的错误,但我找不到我错在哪里......我将 springboot 与 JPA 和 h2 数据库结合使用

预先感谢您的帮助!

这是我的代码:

型号:

@Entity
public class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column (name = "name", nullable = false, unique = false)
private String name;
@Column (name = "firstname", nullable = false, unique = false)
private String firstname;
@Column (name = "username", nullable = false, unique = true)
private String username;
@Column (name = "email", nullable = false, unique = true)
private String email;

// Constructors
public User(int id, String name, String firstname, String username, String email) {
super();
this.id = id;
this.name = name;
this.firstname = firstname;
this.username = username;
this.email = email;
}

public User(String name, String firstname, String username, String email) {
super();
this.name = name;
this.firstname = firstname;
this.username = username;
this.email = email;
}

//Getters Setters
public User() {
super();
}

public long getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", firstname=" + firstname + ", Username=" + username + ", email="
+ email + ", getId()=" + getId() + ", getName()=" + getName() + ", getFirstname()=" + getFirstname()
+ ", getUsername()=" + getUsername() + ", getEmail()=" + getEmail() + ", hashCode()=" + hashCode()
+ ", getClass()=" + getClass() + ", toString()=" + super.toString() + "]";
}


}

Controller :

@Controller
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserController {

@Autowired
UserService UserService;

@GetMapping(value = "/user/{id}")
public Optional<User> findById(@PathVariable int id) {
return UserService.findById(id);
}

@GetMapping(value = "/user/{name}")
public Optional<User> findByName(@PathVariable String name) {
return UserService.findByName(name);
}
}

存储库:

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

Optional <User> findById(int id);

Optional <User> findByName(String name);

}

服务:

@Service
public class UserService {

@Autowired
static UserRepository UserRepository;

public Optional<User> findById(int id) {
return UserRepository.findById(id);

}
public Optional<User> findByName(String name) {
return UserRepository.findByName(name);
}
}

我存储用户对象的 Data.sql:

INSERT INTO User (ID, name , firstname, userName, email) VALUES
(1 , 'bon', 'jean', 'bjean', 'bjean@gmail.com');

INSERT INTO User (ID, name , firstname, userName, email) VALUES
(2 , 'motte', 'bob', 'bmotte', 'bmotte@gmail.com');

INSERT INTO User (ID, name , firstname, userName, email) VALUES
(3 , 'wose', 'guy', 'gwose', 'gwose@gmail.com');

应用程序属性:

# To See H2 Console in Browser:
# http://localhost:8080/h2-console
# Enabling H2 Console
spring.h2.console.enabled=true

# ===============================
# DATABASE CONNECTION
# ===============================
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=h2
spring.datasource.password=h2

# ===============================
# JPA / HIBERNATE
# ===============================
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

最佳答案

我终于找到了我的问题所在。

感谢您花时间帮助我!

我刚刚删除了我的服务类,并像这样更新了我的 Controller ,它现在直接通过存储库:

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserController {

@Autowired
UserRepository userRepository;

@GetMapping(value = "/user/{id}")
public Optional<User> findById(@PathVariable(required = true) int id) {
return userRepository.findById(id);
}

@GetMapping(value = "/user/name/{name}")
public Optional<User> findByName(@PathVariable(required = true) String name) {
return userRepository.findByName(name);
}
}

关于java - Spring启动问题 "There was an unexpected error (type=Internal Server Error, status=500).",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58833453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com