本文介绍了如果控制器不在 Application.java 包中,则会出现 404 错误.@ComponentScan 不起作用 [已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 Application.java、控制器 (@Controller)、服务 (@Service) 和存储库 (@Repository) 位于不同的包中时,我在使用 spring-boot 设置简单应用程序时遇到问题.

I am having trouble with spring-boot setting up a simple application when the Application.java, controller (@Controller), services (@Service) and repositories (@Repository) are in different packages.

我知道这是一个常见问题,我在这里发现这是在主类中设置 @ComponentScan 注释的问题.

I know it's a common issue and I've found here that it's a matter of setting up the @ComponentScan annotation in the main class.

然而,尽管这样做,当我尝试访问我的 REST 服务时仍然收到 404 错误.

However, despite doing this, I still get a 404 error when I try to access my REST service.

这是我的项目结构:

main
├── java
│   ├── application
│   │   └── Application.java
│   ├── controllers
│   │   └── CategoryController.java
│   ├── dto
│   │   └── ReturnMessage.java
│   ├── entities
│   │   ├── Category.java
│   │   ├── Post.java
│   │   └── User.java
│   ├── repositories
│   │   └── CategoryRepository.java
│   ├── services
│   │   ├── CategoryService.java
│   │   └── CategoryServiceImpl.java
│   └── utils
│       └── Constants.java

这是我的 Application.java :

Here's my Application.java :

package application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@SpringBootApplication
@Configuration
@ComponentScan("main.java")
public class Application {

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

}

以及我的 CategoryController.java 的开头:

And the beginning of my CategoryController.java :

@RestController
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * Retourne la liste des catégories
     */
    @RequestMapping(value = Constants.REST_BASE_URL + "/categories", method = RequestMethod.GET)
    public List<Category> readAll() {
        return categoryService.readAll();
    }

Constants.REST_BASE_URL 是rest/",当我调用 localhost:8080/rest/categories 时出现 404 错误.我尝试删除基本 URL,但没有任何改变.如果我将控制器放在 Application.java 类中,它会起作用.

Constants.REST_BASE_URL is "rest/", and I get a 404 error when I call localhost:8080/rest/categories. I tried removing the base URL and it changed nothing. It works if I put the controller in the Application.java class.

任何关于为什么它不起作用的猜测?我是 Spring-boot 的新手,所以可能有一些明显的事情我忘了做.

Any guess as to why it is not working ? I'm new to Spring-boot so there may be something obvious that I forgot to do.

谢谢.

推荐答案

问题在于您的 @ComponentScan 注释.您已将其配置为在名为 main.java 的包中查找,但是您的应用程序中没有这样的包.您拥有的软件包是:

The problem is your @ComponentScan annotation. You've configured it to look in a package named main.java, however you have no such package in your application. The packages that you do have are:

  • 应用
  • 控制器
  • dto
  • 实体
  • 存储库
  • 服务
  • utils

您可以列出所有这些包:

You could list all of these packages:

@ComponentScan({"application", "controllers", "dto", "entities", "repositories", "services", "utils"})

然而,这将是非常规的.

However, this would be rather unconventional.

您的所有包都有一个共同的根是更传统的做法.如果您将 Application 类放在这个包中,则根本不需要显式的 @ComponentScan.像这样:

It's more conventional for all of your packages to have a common root. If you place your Application class in this package there's then no need for an explicit @ComponentScan at all. Something like this:

main
├── java
|   |   foo
│   │   ├── Application.java
|   │   ├── controllers
|   │   │   └── CategoryController.java
|   │   ├── dto
|   │   │   └── ReturnMessage.java
|   │   ├── entities
│   │   ├── Category.java
│   │   ├── Post.java
│   │   ├── User.java
|   │   ├── repositories
|   │   │   └── CategoryRepository.java
|   │   ├── services
|   │   │   ├── CategoryService.java
|   │   │   └── CategoryServiceImpl.java
|   │   └── utils
|   │       └── Constants.java

您的软件包名称现在将是:

Your package names would now be:

  • foo
  • foo.controllers
  • foo.dto
  • foo.entities
  • foo.repositories
  • foo.services
  • foo.utils

并且您的 Application 类位于 foo 包中,这意味着已为其及其所有子包启用组件扫描,而无需显式配置它.

And your Application class being in the foo package will meant that component scanning is enabled for it and all of its sub-packages without you having to explicitly configure it.

这篇关于如果控制器不在 Application.java 包中,则会出现 404 错误.@ComponentScan 不起作用 [已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:24