我使用spring-boot启动了一个项目,实际上我正在尝试在我的本地主机端口上获取JSON(如果我理解)信息。但是我遇到了这个错误:“内部伺服错误,状态= 500”
我在某个地方犯了一个愚蠢的错误,但我找不到我做错了什么地方... 我正在使用带有JPA和h2数据库的springboot
在此先感谢您的帮助!
这是我的代码:
型号:
@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
@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');
application_properties:
# 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
答案 0 :(得分:1)
我认为您必须更改GetMapping值,例如:
@GetMapping(value = "/user/{id}")
public Optional<User> findById(@PathVariable int id) {
return UserService.findById(id);
}
@GetMapping(value = "/user/by-name/{name}")
public Optional<User> findByName(@PathVariable String name) {
return UserService.findByName(name);
}
或
@GetMapping(value = "/user/")
public Optional<User> findByName(@RequestParam(name="name") String name) {
return UserService.findByName(name);
}
并且它可以与相对URL:/user/?name=bon
答案 1 :(得分:1)
我终于找到了我的问题。
感谢您度过的时间来帮助我!
我刚刚删除了服务类,并像这样更新了控制器,它现在直接通过存储库传递:
@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);
}
}