我是springboot的新手,我已经用springboot创建了一个多模块项目(maven)。我创建了一些restapi并连接到h2数据库。数据库已成功连接并能够在localhost中运行。
这是我的项目树。。用户管理是父模块和核心模块,serverapi是子模块。我为每个模块创建了包,并添加了相关的类。
我已经尝试了我所知道的一切,在谷歌上搜索了5天,但没有任何效果。我已经包括了我在这里写的每一个代码。请帮我找出问题所在(我正在使用intellij idea 2020.3(终极版)
用户.java
package com.hms.usermanagement.core.model;
import javax.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "full_name")
private String fullName;
@Column(name = "email")
private String email;
public User() {
}
public User(long id, String fullName, String email) {
this.id = id;
this.fullName = fullName;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}}
用户存储库
package com.hms.usermanagement.core.repository;
import com.hms.usermanagement.core.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
应用
package com.hms.usermanagement.serverAPI.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.run(args);
}}
用户控制器
package con.hms.usermanagement.serverAPI.controller;
import com.hms.usermanagement.core.model.User;
import com.hms.usermanagement.core.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserRepository userRepository;
//Create Users
@PostMapping("/user")
public User createUser( @Validated @RequestBody User user){
return userRepository.save(user);
}
//View all Users
@GetMapping("/users")
public List<User> getAllUsers(){
return userRepository.findAll();
}
//Update Users
@PutMapping("/users/{id}")
public ResponseEntity <User> updateUser(@PathVariable(value = "id") long userId , @RequestBody User userDetails){
Optional<User> user = userRepository.findById(userId);
if(user.isPresent()){
User _user = user.get();
_user.setFullName(userDetails.getFullName());
_user.setEmail(userDetails.getEmail());
return new ResponseEntity<>(userRepository.save(_user), HttpStatus.OK);
}else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
//Delete Users
@DeleteMapping("/users/{id}")
public ResponseEntity<?> deleteUser(@PathVariable(value = "id") long userId){
userRepository.findById(userId);
userRepository.deleteById(userId);
return ResponseEntity.ok().build();
}
应用程序属性
spring.datasource.url=jdbc:h2:~/test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# enable H2 console
spring.h2.console.enabled=true
# custom H2 console
spring.h2.console.path=/h2
架构-h2.sql
CREATE TABLE users (id long PRIMARY KEY AUTO_INCREMENT, full_name VARCHAR(30), email VARCHAR(50));
我试过使用这两个网址
即使“id”字段是自动生成的,但我试图添加id也使用 Postman 。。但仍然得到同样的错误
2条答案
按热度按时间busg9geu1#
你的sprint boot runner课程,
Application
班级人数不足com.hms.usermanagement.serverAPI.application
包,因此spring boot将只扫描com.hms.usermanagement.serverAPI.application
. 所以SpringBoot无法识别您的核心和web组件。为了解决这个问题,试着搬家
Application.java
下课com.hms.usermanagement
.也可以通过添加
@ComponentScan
注解到Application.java
班级:z9smfwbn2#
删除@validated并按如下所示尝试@validated
使用[localhost]:[port]/api/user调用