Getting "No message available" error with Spring Boot + REST application
Three Possible Solutions:
1) Make sure the YourController.java file that has the @Controller and the YourSpringBootFile.java file that has the @SpringBootApplication are in the same package.
For example, this is wrong:
This is the right way:
So you know what I'm talking about, here is my WebController.java file:
@RestController
public class WebController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping(value= "/hi", method = RequestMethod.GET)
public @ResponseBody Greeting sayHello(
@RequestParam(value = "name", required = false, defaultValue = "Stranger") String name) {
System.out.println("Inside sayHello() of WebController.java");
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
Here is my JsonPostExampleProj1Application.java:
@SpringBootApplication
public class JsonPostExampleProj1Application {
public static void main(String[] args) {
SpringApplication.run(JsonPostExampleProj1Application.class, args);
}
}
2) If you want your controller to be in a different package outside of YourSpringBootFile.java's package, then follow these instructions = Spring: Run multiple "SpringApplication.Run()" in application main method
3) Try using @RestController instead of @Controller on top of your Controller class.
This can help someone as it was in my case.
Make sure the package name of the controller is the derived (or child) package of your Spring main method package.
For example:
If the main method package is com.company.demo.example
then the controller package should be like com.company.demo.example.controller
(if you specify something like com.company.demo.controller
it won't work!).
You're probably missing @SpringBootApplication
:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication
includes @ComponentScan
which scans the package it's in and all children packages. Your controller may not be in any of them.
I just came across this error and none of the solutions above worked for me, so im adding another posible thing that perhaps you can be missing too, make sure that you have annotation @ResponseBody
on your method.
@RequestMapping(value="/yourPath", method=RequestMethod.GET)
@ResponseBody
public String exampleMethod() {
return "test";
}