Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Micronaut controller error Page Not Found

Tags:

micronaut

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
like image 900
Khwaja Sanjari Avatar asked Nov 19 '18 18:11

Khwaja Sanjari


2 Answers

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.

like image 111
Jeff Scott Brown Avatar answered Oct 31 '22 20:10

Jeff Scott Brown


I struggled the whole day just to find that its a bug in Eclipse and the issue has been resolved in the latest version. Please switch to Version: 2020-12 (4.18.0) or above

Issue Description

Eclipse Bug

like image 38
Rituparna Bhattacharyya Avatar answered Oct 31 '22 21:10

Rituparna Bhattacharyya