Search code examples
javamicronaut

Micronaut controller error Page Not Found


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

Solution

  • 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.