【问题标题】:Spring boot controller fails to hit error 404Spring Boot 控制器无法命中错误 404
【发布时间】:2020-04-21 05:58:12
【问题描述】:

我正在使用 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("l.gaga@ymail.com");
        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

【问题讨论】:

    标签: java spring-boot multi-module


    【解决方案1】:

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

    所以要么删除这个并将包放在你有@SpringBootApplication的包下面,或者将所有包添加到@ComponentScan

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

    所以删除 ComponentScan 并将 RestServicesApplication 移动到 com.sample

    【讨论】:

    • @SpringBootApplicationcom.sample.rest 包中,@Repositorycom.sample.user 包中,所以我不明白你的意思,放在我有的包下面 `@SpringBootApplication`。它下面没有任何东西,因为它们在不同的包中。感谢任何澄清:)
    • 根据@SimonMartinelli 所说,将所有包放入com.sample.rest 中,然后spring boot 会自动检测com.sample.rest 中的所有包。您不需要然后组件扫描注释。或使用 @ComponentScan(basePackages = "com.sample") 以便启动将检测 com.sample 之后的所有包
    • @SimonMartinelli 感谢您的更新,这是有道理的。但是我们不能以目前的方式解决问题吗?您的解决方案似乎完全重做
    • 这不是一个完整的重做。它只是在另一个包中移动一个类。但是也可以看看@Kandy 的解决方案
    【解决方案2】:

    使用它可以解决您的问题。

    @ComponentScan(basePackages = "com.sample")

    或使用多个包 sacn 之类的东西

    @ComponentScan({ "com.sample", "com.sample.user" })

    【讨论】:

      猜你喜欢
      • 2020-10-11
      • 2018-10-04
      • 2016-09-17
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2019-08-14
      相关资源
      最近更新 更多