Getting “No message available” error with Spring Boot + REST application

后端 未结 10 2403
感动是毒
感动是毒 2020-12-30 02:02

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

@RestController
public class GreetingsController {
    @RequestMappin         


        
相关标签:
10条回答
  • 2020-12-30 02:17

    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.

    0 讨论(0)
  • 2020-12-30 02:18

    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.

    0 讨论(0)
  • 2020-12-30 02:20

    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.

    0 讨论(0)
  • 2020-12-30 02:22

    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.

    0 讨论(0)
  • 2020-12-30 02:22

    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";
    }
    
    0 讨论(0)
  • 2020-12-30 02:24

    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

    0 讨论(0)
提交回复
热议问题