英文:
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/hello
或http://localhost:8080/symbol/hello
时,它显示"Not Found"错误。这可能与路由配置或请求路径有关。您可以检查路由配置和请求路径是否正确设置,以确保控制器可以正确处理请求。
英文:
Here is the 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.*")
}
}
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("/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";
}
}
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 Launch,Micronaut 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("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") // Not needed
runtimeOnly("ch.qos.logback:logback-classic")
implementation 'net.datafaker:datafaker:1.9.0'
// runtimeOnly("io.micronaut:micronaut-http-server-netty") // Not needed
// runtimeOnly("io.netty:netty-codec-http:4.1.66.Final") // Not needed
// runtimeOnly("io.netty:netty-handler:4.1.66.Final") // Not needed
}
application {
mainClass.set("com.micronaut.Application")
}
java {
sourceCompatibility = JavaVersion.toVersion("17")
targetCompatibility = JavaVersion.toVersion("17")
}
graalvmNative.toolchainDetection = false
micronaut {
runtime("netty") // Add
testRuntime("junit5")
processing {
incremental(true)
annotations("com.micronaut.*")
}
}
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") // Not needed
runtimeOnly("ch.qos.logback:logback-classic")
implementation 'net.datafaker:datafaker:1.9.0'
// runtimeOnly("io.micronaut:micronaut-http-server-netty") // Not needed
// runtimeOnly("io.netty:netty-codec-http:4.1.66.Final") // Not needed
// runtimeOnly("io.netty:netty-handler:4.1.66.Final") // Not needed
}
application {
mainClass.set("com.micronaut.Application")
}
java {
sourceCompatibility = JavaVersion.toVersion("17")
targetCompatibility = JavaVersion.toVersion("17")
}
graalvmNative.toolchainDetection = false
micronaut {
runtime("netty") // Add
testRuntime("junit5")
processing {
incremental(true)
annotations("com.micronaut.*")
}
}
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("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") // Not needed
runtimeOnly("ch.qos.logback:logback-classic")
implementation 'net.datafaker:datafaker:1.9.0'
// runtimeOnly("io.micronaut:micronaut-http-server-netty") // Not needed
// runtimeOnly("io.netty:netty-codec-http:4.1.66.Final") // Not needed
// runtimeOnly("io.netty:netty-handler:4.1.66.Final") // Not needed
}
application {
mainClass.set("com.micronaut.Application")
}
java {
sourceCompatibility = JavaVersion.toVersion("17")
targetCompatibility = JavaVersion.toVersion("17")
}
graalvmNative.toolchainDetection = false
micronaut {
runtime("netty") // Add
testRuntime("junit5")
processing {
incremental(true)
annotations("com.micronaut.*")
}
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论