我正在用Hibernate和MySQL构建这个Spring Boot应用程序。我知道这是非常基本的,并且多次询问相同的问题,但是我无法弄清楚为什么控制器没有命中并给出404错误。如我所见,问题出在ComponentScan上,其中我的@SpringBootApplication@RestController驻留在一个程序包中,而我的@Repository@Entity则位于另一个程序包中。
当我将包包含为@ComponentScan(basePackages = "com.sample.user")时,项目将成功构建并运行,但未命中GET方法getUser(),也没有控制台输出错误。 GET方法仅在我省略
控制器类中的@Autowired private UserRepository userRepository;以及应用程序类中的@ComponentScan

控制者

package com.sample.rest.controller;

import com.sample.user.entity.User;
import com.sample.user.repository.UserRepository;
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;

@RestController
@RequestMapping ("user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/")
    public User getUser() {
        User user  = new User();
        user.setFirstName("Lady");
        user.setLastName("Gaga");
        user.setEmail("[email protected]");
        userRepository.save(user);
        return user;
    }
}


应用

package com.sample.rest;

import com.sample.rest.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = "com.sample.user")
public class RestServicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestServicesApplication.class, args);
    }

}


储存库介面

package com.sample.user.repository;

import com.sample.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;

@EnableJpaRepositories
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}


实体

package com.sample.user.entity;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name", nullable = false)
    private String lastName;

    @Column(name = "email_address", nullable = false)
    private String 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;
    }
}


浏览器页面
https://ibb.co/KDsqLn3

最佳答案

使用@ComponentScan(basePackages = "com.sample.user")覆盖默认行为。

因此,要么删除此文件,然后将软件包放在您具有@SpringBootApplication的软件包下面,要么将所有软件包添加到@ComponentScan

我建议不要使用默认的Spring Boot行为。

因此,删除ComponentScanRestServicesApplication移至com.sample

关于java - Spring Boot Controller 无法达到错误404,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61336852/

10-10 17:10