7

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
saw303
  • 8,051
  • 7
  • 50
  • 90
Khwaja Sanjari
  • 455
  • 5
  • 17

2 Answers2

9

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.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • 1
    It is impossible to tell from your code sample if you are using Java or Groovy. The sample I linked is using Java. If you are using Groovy, of course you need the `micronaut-inject-groovy` dependency. – Jeff Scott Brown Nov 19 '18 at 18:23
  • 2
    Thanks for the quick response. My bad I keep on forgetting to enable annotation processor. Thanks for pointing it out. I would like to know if there is a way to enable console debug logging in IDE for micronaut similar to spring boot. – Khwaja Sanjari Nov 19 '18 at 19:09
  • "I would like to know if there is a way to enable console debug logging in IDE for micronaut similar to spring boot.". Yes you can. See https://github.com/jeffbrown/khwaja404/blob/master/src/main/resources/logback.xml. – Jeff Scott Brown Nov 19 '18 at 19:21
  • It is not working with eclipse. I ran the application using eclipse and same error. Do I need to enable annotation processing in eclipse? I have never done that for spring boot applications. – Khwaja Sanjari Nov 19 '18 at 19:22
  • "Do I need to enable annotation processing in eclipse?" - yes. – Jeff Scott Brown Nov 19 '18 at 19:22
  • "I have never done that for spring boot applications." - Spring Boot doesn't depend on compile time annotation processors. Micronaut does. – Jeff Scott Brown Nov 19 '18 at 19:23
  • 1
    If you don't know how to enable compile time annotation processors in Eclipse, you should post that as a separate question. – Jeff Scott Brown Nov 19 '18 at 19:24
  • 1
    If you checkout the project I linked and run `./gradlew run` (or `gradlew run` if you are using the basic Windows shell), you will see that the Micronaut endpoint works fine. – Jeff Scott Brown Nov 19 '18 at 19:25
  • If you are using IntelliJ, in addition to enabling annotation processor, in the run configuration, you will need to set "Run Gradle task", "run" as a build step. (This is also documented in the getting started documentation) – bond Mar 15 '19 at 03:55
  • I had the same issue and I was running my app from IntellijIDEA. As mention above I enabled the annotation processors and worked for me. Go TO: Project Settings->Build, Execution, Deployment->Compiler-> Annotation Processors and check the Enable annotation processing box. – Drisal Jun 05 '19 at 03:22
  • "If you are using IntelliJ, in addition to enabling annotation processor, in the run configuration, you will need to set Run Gradle task, "run" as a build step." - I do not think that is true. There are reasons you might want to do that, but it is not the case that you need to do that in general. – Jeff Scott Brown Jun 06 '19 at 17:30
  • For Eclipse IDE after configure **Automatically configure JDT APT** run: `Maven ->Update Project...` and after that, run: `Build Project` – Alisson Gomes Nov 22 '19 at 05:49
1

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