Micronaut controller error Page Not Found

こ雲淡風輕ζ 提交于 2021-01-27 07:37:33

问题


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

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";
   }
}

But I am getting

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

whenever I try to access the /hello end point

My application.yml looks like this:

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

回答1:


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";
    }
}

The endpoint responds:

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

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.

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



来源:https://stackoverflow.com/questions/53380347/micronaut-controller-error-page-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!