Micronaut 控制器错误页面未找到 [英] Micronaut controller error Page Not Found

查看:104
本文介绍了Micronaut 控制器错误页面未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的 micronaut 应用程序mn create-app example.micronaut.complete

I created a new micronaut app using mn create-app example.micronaut.complete

之后,我使用intellij打开了项目,并使用以下代码向项目添加了一个新类作为TestController:

After that I opened the project using intellij and added a new class as TestController to the project with code below:

package example.micronaut;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;


@Controller("/hello")
public class TestController {

TestController(){}

@Get(value = "/", produces = MediaType.TEXT_PLAIN)
String getTest(){
    return "some string";
   }
}

但是我得到了

{"_links":{"self":{"href":"/","templated":false}},"message":"Page Not Found"}

每当我尝试访问/hello 端点

whenever I try to access the /hello end point

我的 application.yml 如下所示:

My application.yml looks like this:

micronaut:
    application:
        name: complete
    server:
        port: 8080

推荐答案

如果没有更多地了解您的项目,就很难说出哪里出了问题.我已将您的代码直接粘贴到一个项目中,它按预期工作.请参阅 https://github.com/jeffbrown/khwaja404 上的项目.https://github.com/jeffbrown/khwaja404/blob/a3e57623ed5b30e28eb95bfe0f4a4a5c9d123fd8/src/main/java/example/micronaut/TestController.java 工作正常...

Without seeing more of your project it is hard to say what is wrong. I have pasted your code directly into a project and it works as expected. See the project at https://github.com/jeffbrown/khwaja404. The controller at https://github.com/jeffbrown/khwaja404/blob/a3e57623ed5b30e28eb95bfe0f4a4a5c9d123fd8/src/main/java/example/micronaut/TestController.java works fine...

package example.micronaut;

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


@Controller("/hello")
public class TestController {

    // this empty constructor is not
    // needed, but isn't a problem...
    TestController() {
    }

    @Get(value = "/", produces = MediaType.TEXT_PLAIN)
    String getTest() {
        return "some string";
    }
}

端点响应:

$ curl http://localhost:8080/hello
some string

需要注意的一件事是您可能缺少 micronaut-inject-java 和/或 micronaut-inject 依赖项,如 https://github.com/jeffbrown/khwaja403204040404b304b304b30404b304b3040404040404b304b304040404b304b3040404b30404040404b3040404b30404b30404b304b30404b304040404040404004004004004000000000000000000000000000000000000000000000000000000000000000000L29.

One thing to look for is you may be missing the micronaut-inject-java and/or micronaut-inject dependency as expressed at https://github.com/jeffbrown/khwaja404/blob/a3e57623ed5b30e28eb95bfe0f4a4a5c9d123fd8/build.gradle#L27-L29.

另一个是,如果您从 IDE(如 IntelliJ IDEA)运行应用程序,请确保在构建中启用了注释处理器.

Another is if you are running the app from the IDE (like IntelliJ IDEA), make sure you have annotation processors enabled in the build.

这篇关于Micronaut 控制器错误页面未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆