I get Error: Not Found (Spring with module dependency)

huangapple go评论83阅读模式
英文:

Multimodule gradle project I get Error: Not Found (Spring with module dependency)

问题

在使用Java和Gradle进行多模块项目的学习和实现时,我在发出请求时遇到了以下错误:

Postman

{
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/tasks/hello"
}

在运行命令 ./gradlew :task-service:bootRun 时,项目似乎正常启动,输出 "Tomcat started on port(s): 8080 (http) with context path''"。但是,在发出请求时,我得到了 "not found"。我无法确定错误出现在哪里,但我注意到 "domain" 模块的导入不正确,或者我不知道如何配置它。你能帮我吗?

该项目及其模块分组为 group 'com.nyuro'

nyuro-backend(根项目)
 * domain
 * task-service

"domain" 模块包含实体,如 Task 类。服务将是带有 @Controller、@Service、@Repository 的 Spring Boot 应用程序。在这个例子中是 "task-service"。

以下是主要设置的片段。希望这足以指出一些配置错误。或者你可以在 GitHub 上找到代码:项目链接

settings.gradle(nyuro-backend)

rootProject.name = 'nyuro-backend'
include 'domain'
include 'task-service'

build.gradle(nyuro-backend)

plugins {
    id 'java'
    id 'application'
}

group 'com.nyuro'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}

build.gradle(:domain)

plugins {
    id 'java'
    id 'application'
}

group 'com.nyuro'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
}

test {
    useJUnitPlatform()
}

build.gradle(:task-service)

plugins {
	id 'java'
	id 'application'
	id 'org.springframework.boot' version '3.1.0'
	id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.nyuro'
version = '0.0.1-SNAPSHOT'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	runtimeOnly 'com.mysql:mysql-connector-j'
	implementation 'org.springframework.boot:spring-boot-starter-actuator'
	implementation project(':domain')
}

tasks.named('test') {
	useJUnitPlatform()
}

TaskServiceApplication.java

@EntityScan({"com.nyuro.domain"})
@EnableJpaRepositories({"com.nyuro.domain"})
@ComponentScan({"com.nyuro.domain"})
@SpringBootApplication
public class TaskServiceApplication {

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

}

似乎存在导入 domain.models.Task 模块的问题。我还对 @EntityScan、@EnableJpaRepositories 和 @ComponentScan 进行了多次测试。没有令人满意的结果。

英文:

When studying and implementing a multimodule project with Java and Gradle, I am having the following error when making a request:

Postman

{
     "status": 404,
     "error": "Not Found",
     "message": "No message available",
     "path": "/tasks/hello"
}

When running the command "./gradlew :task-service:bootRun" the project seems to start normally "Tomcat started on port(s): 8080 (http) with context path''. But when making requests, I get "not found". I can't say where the error is, but I realize that the import of the "domain" module is not correct or I don't know how to configure it. Can you help me?

The project and its modules - group 'com.nyuro'

nyuro-backend (root-project)
 *domain
 *task-service

The "domain" module contains entities, such as the Task class. Services will be Spring Boot app with @Controller, @Service, @Repository. In this example "task-service".

Here are the main snippets of settings. I hope that's enough to point me out some configuration error. Or here is the code on github: Project Link Here

settings.gradle(nyuro-backend)

rootProject.name = 'nyuro-backend'
include 'domain'
include 'task-service'

build.gradle(nyuro-backend)

plugins {
    id 'java'
    id 'application'
}

group 'com.nyuro'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}

build.gradle(:domain)

plugins {
    id 'java'
    id 'application'
}

group 'com.nyuro'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
}

test {
    useJUnitPlatform()
}

build.gradle(:task-service)

plugins {
	id 'java'
	id 'application'
	id 'org.springframework.boot' version '3.1.0'
	id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.nyuro'
version = '0.0.1-SNAPSHOT'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	runtimeOnly 'com.mysql:mysql-connector-j'
	implementation 'org.springframework.boot:spring-boot-starter-actuator'
	implementation project(':domain')
}

tasks.named('test') {
	useJUnitPlatform()
}

TaskServiceApplication.java

@EntityScan({"com.nyuro.domain"})
@EnableJpaRepositories({"com.nyuro.domain"})
@ComponentScan({"com.nyuro.domain"})
@SpringBootApplication
public class TaskServiceApplication {

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

}

There seems to be a problem importing the domain.models.Task module. I also did several tests on @EntityScan, @EnableJpaRepositories, @ComponentScan imports. No satisfactory result.

答案1

得分: 0

Your 404 Not Found error comes solely from your Controller not being picked up because of custom ComponentScan annotation on SpringBootApplication but let me raise a few more points:

  1. 您的 404 未找到错误仅源自您的 Controller 未被拾取,因为 SpringBootApplication 上有自定义 ComponentScan 注解,但让我提出更多观点:

  2. 您不应将 .gradle.ideabuild 等目录提交到 Git。创建一个 .gitignore 文件来忽略它们 - 您已经有一个,但它在 task-service 内部,最好在根目录中也有一个(但您可以更具体地针对某些特定的子项目创建一个)。

  3. 您有两个 gradle wrappers(在根目录和 task-service 中) - task-service 中的那一个应该被删除。

  4. 我认为您不需要 task-service/settings.gradle 文件。我意识到您可能最初是从单模块项目开始的,然后将其转变为多模块项目。

  5. 您正在对 com.nyuro.domain 执行 ComponentScanEntityScan,但在此包中没有类(领域类位于 module 包中),最重要的是 @SpringBootApplication 已经在其所在的包上执行了组件扫描。您应该删除 @ComponentScan(“com.nyuro.domain”),因为它会阻止 Spring 拾取不在该包下的 Controller(SpringBootApplication 位于 com.nyuro.taskservice,Controller 位于 com.nyuro.taskservice.controllers,所以默认的组件扫描会拾取它,因为它是一个子包)。如果您想要扫描其他包,您将不得不添加 task-service 包 - @ComponentScan({"com.nyuro.domain", "com.nyuro.taskservice"})

  6. 您的数据库设置对我来说不起作用,我不得不将其删除才能使此应用程序启动。

英文:

Your 404 Not Found error comes solely from your Controller not being picked up because of custom ComponentScan annotation on SpringBootApplication but let me raise a few more points:

  1. You should not commit .gradle, .idea, build, etc. directories to Git. Create a .gitignore file to ignore them - you have one but its inside task-service, you should have one in root directory ideally (but you can have more specific to some particular sub-projects)
  2. You have 2 gradle wrappers (in root and task-service) - the one in task-service should be removed
  3. I don’t think you need task-service/settings.gradle file as well. I realise you probably started with a single-module project and turned it into a multi-module one.
  4. You are making ComponentScan and EntityScan on com.nyuro.domain but you have no classes in this package (domain classes are in module package), most importantly @SpringBootApplication already does component scan on the package it is located in. You should remove @ComponentScan(“com.nyuro.domain”) as it prevents you Controller which is not under that package from being picked up by Spring (SpringBootApplication is in com.nyuro.taskservice and controller in com.nyuro.taskservice.controllers so the default component scan will pick it up as its a sub-package). If you want to scan additional packages you will have to add task-service package as well - @ComponentScan({"com.nyuro.domain", "com.nyuro.taskservice"}).
  5. You database setup is not working for me, I had to remove it to make this app start


huangapple
  • 本文由 发表于 2023年6月5日 09:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定