找不到Spring Boot错误404休眠

时间:2019-12-19 15:29:45

标签: java spring hibernate spring-boot postman

我是Spring Boot的新手,我只是想通过此代码从数据源获取/获取数据:

  

EmployeController.java

@RestController
@RequestMapping("/api")

    public class EmployeController {

        @Autowired
        private EmployeService employeService;

        @GetMapping("/employe")
        public List<Employe> get(){
            return employeService.get();
        }
    }
  

Employe.java

@Entity
@Table(name="employe")
public class Employe {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private String gender;
    @Column
    private String department;
    @Column
    private Date dob;
    public int 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 getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public Date getDob() {
        return dob;
    }
    public void setDob(Date dob) {
        this.dob = dob;
    }
    @Override
    public String toString() {
        return "Employe [id=" + id + ", name=" + name + ", gender=" + gender + ", department=" + department + ", dob="
                + dob + "]";
    }
  

EmployeService.java

public interface EmployeService {
    List<Employe> get();
    Employe get(int id);
    void save(Employe employe);
    void delete(int id);
}
  

EmployeServiceImplement.java

@Service
public class EmployeServiceImplement implements EmployeService {

    @Autowired
    private EmployeDAO employeDAO;

    @Transactional
    @Override
    public List<Employe> get() {
        return employeDAO.get();
    }
}
  

EmployeDAO.java

public interface EmployeDAO {
    List<Employe> get();
    Employe get(int id);
    void save(Employe employe);
    void delete(int id);
}
  

EmployeDAOimplement.java

@Repository
public class EmployeDAOImplement implements EmployeDAO {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<Employe> get() {
        Session currentSession = entityManager.unwrap(Session.class);
        Query<Employe> query = currentSession.createQuery("from Employe", Employe.class);
        List<Employe>list = query.getResultList();  
        return list;
    }
}

我已将与MySQl数据库有关的所有配置都写入application.properties,并在数据库中创建了一个employeee表。

据我所知,Spring Data JPA的实现方式远胜于此,但是我只是想以相同的方式开始……当我以Spring Boot App的身份运行项目,然后从Postman运行时,出现了404错误这个:

enter image description here

我无法理解为什么这会显示404错误以及如何解决。

4 个答案:

答案 0 :(得分:1)

您需要在请求URI中添加工件的名称(项目名称)。例如:

http://localhost:8080/my-app/api/employe

如果您不想在URI中使用它,请在application.properties中更改contextPath配置:

server.contextPath = /

答案 1 :(得分:1)

控制器@RequestMapping("/api")上的注释表示路由将为http://localhost:8080/api,但是您正在尝试调用http://localhost:8080/api/employe。我认为您应该将其更改为@RequestMapping("/api/employe")

答案 2 :(得分:1)

您也可以这样:

在您的应用道具文件中设置:

server.servlet.context-path=/api

然后删除RequestMapping注释:

@RestController
public class EmployeController {

    @Autowired
    private EmployeService employeService;

    @GetMapping("/employe")
    public List<Employe> get(){
        return employeService.get();
     }
 }

那么您应该可以使用请求的URL检索它

答案 3 :(得分:1)

application.properties中的上下文路径配置是什么?仅在设置属性后,您才会看到与此server.servlet.context-path相同的属性。如果未设置,则路径http://localhost:8080/api/employe可能是正确的,在这种情况下,我建议您检查应用程序是否确实在端口8080上运行,是否有其他应用程序在端口{{ 1}},并且您的应用程序在其他端口上运行,那么您肯定会收到404响应。