Micronaut java gradle project can't detect the controller , testing any route the response return Not found

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

Micronaut java gradle project can't detect the controller , testing any route the response return Not found

问题

以下是build.gradle文件的中文翻译部分:

plugins {
    id("com.github.johnrengelman.shadow") version "7.1.2"
    id("io.micronaut.application") version "3.7.9"
}

version = "0.1"
group = "com.micronaut"

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor("io.micronaut:micronaut-http-validation")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut:micronaut-jackson-databind")
    implementation("jakarta.annotation:jakarta.annotation-api")
    implementation("javax.inject:javax.inject:1")
    runtimeOnly("ch.qos.logback:logback-classic")
    implementation 'net.datafaker:datafaker:1.9.0'
    runtimeOnly("io.micronaut:micronaut-http-server-netty")
    runtimeOnly("io.netty:netty-codec-http:4.1.66.Final")
    runtimeOnly("io.netty:netty-handler:4.1.66.Final")
}

application {
    mainClass.set("com.micronaut.Application")
}

java {
    sourceCompatibility = JavaVersion.toVersion("17")
    targetCompatibility = JavaVersion.toVersion("17")
}

graalvmNative.toolchainDetection = false
micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.micronaut.*")
    }
}

以下是控制器实现的中文翻译部分:

package com.micronaut.broker;

import java.util.ArrayList;
import java.util.List;

import com.micronaut.broker.data.InMemoryStore;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/symbol")
public class SymbolController {

	private final InMemoryStore inMemoryStore;

	public SymbolController(InMemoryStore inMemoryStore) {
		this.inMemoryStore = inMemoryStore;
	}

	@Get("/")
	public List<Symbol> getAll() {

		return new ArrayList<>(inMemoryStore.getSymbols().values());
	}

	@Get("/hello")
	public String hello() {

		return "hello";
	}
}

关于您提到的问题,您尝试过调试代码,但控制器中的断点没有被触发。您还尝试更改服务器和Netty服务器的版本和依赖项,但当访问http://localhost:8080/symbol/hellohttp://localhost:8080/symbol/hello时,它显示"Not Found"错误。这可能与路由配置或请求路径有关。您可以检查路由配置和请求路径是否正确设置,以确保控制器可以正确处理请求。

英文:

Here is the build gradle

plugins {
    id(&quot;com.github.johnrengelman.shadow&quot;) version &quot;7.1.2&quot;
    id(&quot;io.micronaut.application&quot;) version &quot;3.7.9&quot;
}

version = &quot;0.1&quot;
group = &quot;com.micronaut&quot;

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor(&quot;io.micronaut:micronaut-http-validation&quot;)
    implementation(&quot;io.micronaut:micronaut-http-client&quot;)
    implementation(&quot;io.micronaut:micronaut-jackson-databind&quot;)
    implementation(&quot;jakarta.annotation:jakarta.annotation-api&quot;)
    implementation(&quot;javax.inject:javax.inject:1&quot;)
    runtimeOnly(&quot;ch.qos.logback:logback-classic&quot;)
    implementation &#39;net.datafaker:datafaker:1.9.0&#39;
    runtimeOnly(&quot;io.micronaut:micronaut-http-server-netty&quot;)
    runtimeOnly(&quot;io.netty:netty-codec-http:4.1.66.Final&quot;)
    runtimeOnly(&quot;io.netty:netty-handler:4.1.66.Final&quot;)
}

application {
    mainClass.set(&quot;com.micronaut.Application&quot;)
}

java {
    sourceCompatibility = JavaVersion.toVersion(&quot;17&quot;)
    targetCompatibility = JavaVersion.toVersion(&quot;17&quot;)
}

graalvmNative.toolchainDetection = false
micronaut {
    testRuntime(&quot;junit5&quot;)
    processing {
        incremental(true)
        annotations(&quot;com.micronaut.*&quot;)
    }
}

Here is the controller implementation

package com.micronaut.broker;

import java.util.ArrayList;
import java.util.List;

import com.micronaut.broker.data.InMemoryStore;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller(&quot;/symbol&quot;)
public class SymbolController {

	private final InMemoryStore inMemoryStore;

	public SymbolController(InMemoryStore inMemoryStore) {
		this.inMemoryStore = inMemoryStore;
	}

	@Get(&quot;/&quot;)
	public List&lt;Symbol&gt; getAll() {

		return new ArrayList&lt;&gt;(inMemoryStore.getSymbols().values());
	}

	@Get(&quot;/hello&quot;)
	public String hello() {

		return &quot;hello&quot;;
	}
}

I tried to debug the code but it executed directly with out accessing the breakpoints in the controller

  • tried also changes the version and dependencies of the server and the netty server

But everything end with that response when try http://localhost:8080/symbol/hello or that http://localhost:8080/symbol/hello
it show that error

{
"message": "Not Found",
"_links": {
"self": {
"href": "/symbol/hello",
"templated": false
}
},
"_embedded": {
"errors": [
{
"message": "Page Not Found"
}
]
}
}

答案1

得分: 2

First off, I had no trouble running your application with ./gradlew run.

不过,我使用 ./gradlew run 运行您的应用程序时没有遇到任何问题。

Not sure how you created it, did you use Micronaut Launch, Micronaut CLI or via an IDE (IntelliJ and VS Code have plugins)?

不确定您是如何创建它的,您是使用 Micronaut LaunchMicronaut CLI 还是通过 IDE(IntelliJ 和 VS Code 有插件)?

Or did you start with the code from one of the many guides? For example: Creating your First Micronaut Application.

还是您是根据众多的 指南 中的代码开始的?例如:创建您的第一个 Micronaut 应用程序

If you are running from IntelliJ with Gradle, check out Using IntelliJ IDEA to Develop Micronaut Applications.

如果您是在 IntelliJ 中使用 Gradle 运行,请查看 使用 IntelliJ IDEA 开发 Micronaut 应用程序

build.gradle

plugins {
    id(&quot;com.github.johnrengelman.shadow&quot;) version &quot;7.1.2&quot;
    id(&quot;io.micronaut.application&quot;) version &quot;3.7.9&quot;
}

version = &quot;0.1&quot;
group = &quot;com.micronaut&quot;

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor(&quot;io.micronaut:micronaut-http-validation&quot;)
    implementation(&quot;io.micronaut:micronaut-http-client&quot;)
    implementation(&quot;io.micronaut:micronaut-jackson-databind&quot;)
    implementation(&quot;jakarta.annotation:jakarta.annotation-api&quot;)
//    implementation(&quot;javax.inject:javax.inject:1&quot;) // Not needed
    runtimeOnly(&quot;ch.qos.logback:logback-classic&quot;)
    implementation &#39;net.datafaker:datafaker:1.9.0&#39;
//    runtimeOnly(&quot;io.micronaut:micronaut-http-server-netty&quot;) // Not needed
//    runtimeOnly(&quot;io.netty:netty-codec-http:4.1.66.Final&quot;) // Not needed
//    runtimeOnly(&quot;io.netty:netty-handler:4.1.66.Final&quot;) // Not needed
}

application {
    mainClass.set(&quot;com.micronaut.Application&quot;)
}

java {
    sourceCompatibility = JavaVersion.toVersion(&quot;17&quot;)
    targetCompatibility = JavaVersion.toVersion(&quot;17&quot;)
}

graalvmNative.toolchainDetection = false
micronaut {
    runtime(&quot;netty&quot;) // Add
    testRuntime(&quot;junit5&quot;)
    processing {
        incremental(true)
        annotations(&quot;com.micronaut.*&quot;)
    }
}
plugins {
    id(&quot;com.github.johnrengelman.shadow&quot;) version &quot;7.1.2&quot;
    id(&quot;io.micronaut.application&quot;) version &quot;3.7.9&quot;
}

version = &quot;0.1&quot;
group = &quot;com.micronaut&quot;

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor(&quot;io.micronaut:micronaut-http-validation&quot;)
    implementation(&quot;io.micronaut:micronaut-http-client&quot;)
    implementation(&quot;io.micronaut:micronaut-jackson-databind&quot;)
    implementation(&quot;jakarta.annotation:jakarta.annotation-api&quot;)
//    implementation(&quot;javax.inject:javax.inject:1&quot;) // Not needed
    runtimeOnly(&quot;ch.qos.logback:logback-classic&quot;)
    implementation &#39;net.datafaker:datafaker:1.9.0&#39;
//    runtimeOnly(&quot;io.micronaut:micronaut-http-server-netty&quot;) // Not needed
//    runtimeOnly(&quot;io.netty:netty-codec-http:4.1.66.Final&quot;) // Not needed
//    runtimeOnly(&quot;io.netty:netty-handler:4.1.66.Final&quot;) // Not needed
}

application {
    mainClass.set(&quot;com.micronaut.Application&quot;)
}

java {
    sourceCompatibility = JavaVersion.toVersion(&quot;17&quot;)
    targetCompatibility = JavaVersion.toVersion(&quot;17&quot;)
}

graalvmNative.toolchainDetection = false
micronaut {
    runtime(&quot;netty&quot;) // Add
    testRuntime(&quot;junit5&quot;)
    processing {
        incremental(true)
        annotations(&quot;com.micronaut.*&quot;)
    }
}

I hope this helps! Let me know if you need further assistance.希望这有所帮助!如果需要进一步的帮助,请告诉我。

英文:

First off, I had no trouble running your application with ./gradlew run.

Not sure how you created it, did you use Micronaut Launch, Micronaut CLI or via an IDE (IntelliJ and VS Code have plugins)?

Or did you start with the code from one of the many guides? For example: Creating your First Micronaut Application.

If you are running from IntelliJ with Gradle, check out Using IntelliJ IDEA to Develop Micronaut Applications.

build.gradle

plugins {
    id(&quot;com.github.johnrengelman.shadow&quot;) version &quot;7.1.2&quot;
    id(&quot;io.micronaut.application&quot;) version &quot;3.7.9&quot;
}

version = &quot;0.1&quot;
group = &quot;com.micronaut&quot;

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor(&quot;io.micronaut:micronaut-http-validation&quot;)
    implementation(&quot;io.micronaut:micronaut-http-client&quot;)
    implementation(&quot;io.micronaut:micronaut-jackson-databind&quot;)
    implementation(&quot;jakarta.annotation:jakarta.annotation-api&quot;)
//    implementation(&quot;javax.inject:javax.inject:1&quot;) // Not needed
    runtimeOnly(&quot;ch.qos.logback:logback-classic&quot;)
    implementation &#39;net.datafaker:datafaker:1.9.0&#39;
//    runtimeOnly(&quot;io.micronaut:micronaut-http-server-netty&quot;) // Not needed
//    runtimeOnly(&quot;io.netty:netty-codec-http:4.1.66.Final&quot;) // Not needed
//    runtimeOnly(&quot;io.netty:netty-handler:4.1.66.Final&quot;) // Not needed
}

application {
    mainClass.set(&quot;com.micronaut.Application&quot;)
}

java {
    sourceCompatibility = JavaVersion.toVersion(&quot;17&quot;)
    targetCompatibility = JavaVersion.toVersion(&quot;17&quot;)
}

graalvmNative.toolchainDetection = false
micronaut {
    runtime(&quot;netty&quot;) // Add
    testRuntime(&quot;junit5&quot;)
    processing {
        incremental(true)
        annotations(&quot;com.micronaut.*&quot;)
    }
}

答案2

得分: 0

问题在于Eclipse无法将该项目识别为Gradle项目,因此无法检测到控制器。

以下是适用于Eclipse用户的操作步骤:

  • Eclipse → 窗口 → 显示视图 → 其他 → Gradle任务。

  • 然后,在Gradle任务中导入项目。

  • 展开项目 → Application → 双击运行。

问题应该得到解决。

英文:

The issue is that the eclipse can't read the project as a gradle project so it cannot detect the controller .

Here the steps that worked for me for eclipse users :

  •  Eclipse → Window → Show View → other → Gradle Tasks 

- Then in Gradle Tasks import the project 

  • Expand the project → Application → double click run

 Issue should be solved 

huangapple
  • 本文由 发表于 2023年5月17日 20:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272123.html
匿名

发表评论

匿名网友

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

确定