Getting "No message available" error with Spring Boot + REST application
Asked Answered
G

14

27

I have created demo Spring Boot project and implemented Restful services as shown here

@RestController
public class GreetingsController {
    @RequestMapping(value="/api/greetings", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getGreetings(){
        return new ResponseEntity<String>("Hello World", HttpStatus.OK);
    }
}

When I tried to invoke the service with Postman tool with url "http://localhost:8080/api/greetings" as request method GET, I am getting below error message

{
  "timestamp": 1449495844177,
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/api/greetings"
}

Per Spring Boot application, I don't have to configure the Spring Dispatcher servlet in web.xml.

Can someone help me to find out the missing point here?

Gab answered 7/12, 2015 at 13:52 Comment(1)
The controller looks fine, possibly it is not being wired by the root application class. Are you sure it is being instantiated/wired (add a constructor with a break-point)? Is the package containing the controller class 'under' the root application package (common mistake that I've made several times)?Enginery
K
17

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.

Kurys answered 7/12, 2015 at 14:0 Comment(7)
Thanks for your quick help Cahen. I didn't annotate @ComponentScan in my spring-boot classGab
glad to help :) I believe though that you shouldn't need to annotate with @ComponentScan since @SpringBootApplication already includes itKurys
This answer didn't work for me. There is probably an additional cause of this kind of error. My broken code, as of this date, is at: github.com/djangofan/maven-spring-boot-testing-example . Hopefully I figure out how to fix it today.Wizard
Try with @SpringBootApplication(scanBasePackages = {"com.xxx.yyy"}) Here com.xxx.yyy is base package. Your controller should be inside this packageRamsdell
@KasunKariyawasam Why did I need that? It worked but I wonder why should I need to explicitly tell the SpringBootApplication where my controller classes are.Cathepsin
@KasunKariyawasam I got it. My Spring boot application is in another sibling package than the controller classes. Thats why the main class shall reside either in the same or in the parent package of the controller classes.Cathepsin
I did NOT have my my entities, controllers, services etc. in the same path as @SpringBootApplication main and thats why I had the same issue as the author. This answer helped me - thank you.Maieutic
L
13

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: enter image description here

This is the right way: enter image description here

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.

Lusaka answered 18/12, 2017 at 21:20 Comment(3)
This worked for me: Try using RestController instead of Controller on top of your Controller classDalt
how should look Greeting class?Perrine
What do you mean by "How should look"? Your GreetingsController class is my WebController class.Lusaka
L
8

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!).

Lithophyte answered 7/5, 2019 at 11:8 Comment(1)
This worked for me. I had SpringBootApplication and RestController, and ResponseBody all properly annotated, but controllers would not work in separate package. This renaming scheme fixed it for me.Veer
T
6

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";
}
Tightfisted answered 2/9, 2019 at 21:12 Comment(2)
work perfect :)Fouquiertinville
OP mentioned his controller that has @RestController annotation. This annotation already includes @ResponseBody annotation in its descriptionStructure
F
3

Apart from the anotatting the SpringBoot entry point with @SpringBootApplication((scanBasePackages = "com.duwamish.x.y") so that it includes all the spring components/beans when initialized,

The contextPath also has to be right. If the application is deployed to tomcat with the application name as myapplication see below,

$ ll /usr/local/apache-tomcat-8.0.42/webapps/
total 179216
12495017 drwxrwxrwx  17 urayagppd  NORD\Domain Users       578 Mar  8 11:59 ROOT
12495019 drwxrwxrwx  55 urayagppd  NORD\Domain Users      1870 Mar  8 11:59 docs
12495042 drwxrwxrwx   7 urayagppd  NORD\Domain Users       238 Mar  8 11:59 examples
12495109 drwxrwxrwx   7 urayagppd  NORD\Domain Users       238 Mar  8 11:59 host-manager
12495114 drwxrwxrwx   8 urayagppd  NORD\Domain Users       272 Mar  8 11:59 manager
16169612 drwxr-xr-x   4 urayagppd  NORD\Domain Users       136 May  7 18:47 myapplication
16169594 -rw-r--r--   1 urayagppd  NORD\Domain Users  45340041 May  7 18:47 myapplication.war

Then REST endpoint would be /myapplication/api/greetings

But if the application war is deployed as ROOT, the endpoint resource will be /api/greetings only.

Fermin answered 23/5, 2017 at 18:17 Comment(0)
H
3

If you are using @SpringBootApplication alone, then make sure your rest service controller is in the same package like below -

com.testSpring->SpringBootApplication class
com.testSpring.controller->RestfulController classes
com.testSpring.model->Model classes
..etc

If you are using @ComponentScan(basePackageClasses = UserController.class), then mention specific controller class names.

Hesperian answered 17/11, 2017 at 7:2 Comment(0)
S
1

Actually you need to put the Controller package under same path as your SpringBootApplication java file (spring boot main java class) contains.

com.abc | |---- @SpringBootApplication main java class

com.abc.controller | |---- @RestController class

Sapphirine answered 29/1, 2020 at 13:10 Comment(0)
U
1

I got the same error, but I realized that i was returning String I changed it to ResponseEntity and now it's working perfectly.

Univalence answered 30/6, 2022 at 18:54 Comment(0)
C
0

In my case I called the wrong path via ribbon.

@FeignClient(name = "currency-exchange")
@RibbonClient(name = "currency-exchange")
public interface CurrencyExchangeProxy {

    @GetMapping("/exchange/{from}/to/{to}")
    PairRateDto callForExchangeValue(@PathVariable("from") String fromValue, @PathVariable("to") String toValue);

}

Remote currency-exchange service didn't have handlers for /exchange/{from}/to/{to} path.

So for nonexistent URL I've got 404 which is fair with "No message available" which is strange.

Confiteor answered 19/10, 2018 at 11:57 Comment(0)
M
0

For me I have the same error, but I realise that the path I put to call the api is the problem. If my application name is demo, I have to call http://localhost:9090/AdminProducts to access AdminProducts, not http://localhost:9090/demo/AdminProducts. This name may be use after deploying the war in tomcat.

Murrelet answered 23/10, 2020 at 9:30 Comment(0)
T
0

If “No message available” is accompanied by 404, just check your http request, any syntax issue might be the case.

at least mine was a syntax error in a request while posting with postman.

Tremayne answered 5/4, 2021 at 6:26 Comment(0)
R
0

I also faced this error. All the other suggestions are great.

Just to simplify

If you are using the @Controller

Then make sure you have used the @ResponseBody

If you are using @RestController, the @ResponseBody is not mandatory.

And it should work.

Reimburse answered 2/8, 2023 at 0:12 Comment(0)
M
0

Check if your model, controller, service, repository in the same package as main Java Springbok file (i.e it should be derived package of the Spring main method package.

Magnetite answered 28/10, 2023 at 13:30 Comment(0)
M
-1

all your packages should be after the main package,

like this com.example.demo.* (all your packages)

Maquette answered 7/7, 2019 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.