简体   繁体   English

Spring Boot + REST 应用程序出现“无可用消息”错误

[英]Getting "No message available" error with Spring Boot + REST application

I have created demo Spring Boot project and implemented Restful services as shown here我已经创建了演示 Spring Boot 项目并实现了 Restful 服务,如下所示

@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当我尝试使用带有 URL“ http://localhost:8080/api/greetings ”作为请求方法 GET 的 Postman 工具调用服务时,我收到以下错误消息

{
  "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.对于每个 Spring Boot 应用程序,我不必在 web.xml 中配置 Spring Dispatcher servlet。

Can someone help me to find out the missing point here?有人可以帮我找出这里的缺失点吗?

You're probably missing @SpringBootApplication :您可能缺少@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. @SpringBootApplication包括@ComponentScan扫描它所在的包和所有子包。 Your controller may not be in any of them.您的控制器可能不在其中任何一个中。

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. 1)确保具有@Controller 的YourController.java 文件和具有@SpringBootApplication 的YourSpringBootFile.java 文件在同一个包中。

For example, this is wrong:例如,这是错误的: 在此处输入图像描述

This is the right way:这是正确的方法: 在此处输入图像描述

So you know what I'm talking about, here is my WebController.java file:所以你知道我在说什么,这是我的 WebController.java 文件:

@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:这是我的 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 2) 如果您希望控制器位于 YourSpringBootFile.java 包之外的不同包中,请按照以下说明操作 = Spring:在应用程序主方法中运行多个“SpringApplication.Run()”

3) Try using @RestController instead of @Controller on top of your Controller class. 3) 尝试在 Controller 类之上使用 @RestController 而不是 @Controller。

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.确保控制器的包名是 Spring 主方法包的派生(或子)包。

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!).如果主方法包是com.company.demo.example那么控制器包应该像com.company.demo.example.controller (如果你指定像com.company.demo.controller这样的东西,它就不起作用了!)。

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.我刚遇到这个错误,上面的解决方案都不适合我,所以我添加了另一个可能你也可能错过的东西,确保你的方法上有注释@ResponseBody

@RequestMapping(value="/yourPath", method=RequestMethod.GET)
@ResponseBody
public String exampleMethod() {
return "test";
}

Apart from the anotatting the SpringBoot entry point with @SpringBootApplication((scanBasePackages = "com.duwamish.xy") so that it includes all the spring components/beans when initialized,除了使用@SpringBootApplication((scanBasePackages = "com.duwamish.xy") SpringBoot 入口点以便它在初始化时包含所有弹簧组件/bean,

The contextPath also has to be right. contextPath 也必须是正确的。 If the application is deployed to tomcat with the application name as myapplication see below,如果应用程序部署到tomcat,应用程序名称为myapplication ,如下所示,

$ 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然后 REST 端点将是/myapplication/api/greetings

But if the application war is deployed as ROOT , the endpoint resource will be /api/greetings only.但是如果应用程序战争被部署为ROOT ,端点资源将仅为/api/greetings

If you are using @SpringBootApplication alone, then make sure your rest service controller is in the same package like below -如果您单独使用@SpringBootApplication ,请确保您的休息服务控制器在同一个包中,如下所示 -

 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.如果您使用的是@ComponentScan(basePackageClasses = UserController.class) ,请提及特定的控制器类名称。

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.远程currency-exchange服务没有/exchange/{from}/to/{to}路径的处理程序。

So for nonexistent URL I've got 404 which is fair with "No message available" which is strange.因此,对于不存在的 URL,我得到了 404,这与“没有可用的消息”是公平的,这很奇怪。

Actually you need to put the Controller package under same path as your SpringBootApplication java file (spring boot main java class) contains.实际上,您需要将 Controller 包放在与 SpringBootApplication java 文件(spring boot main java 类)包含的相同路径下。

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

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

For me I have the same error, but I realise that the path I put to call the api is the problem.对我来说,我有同样的错误,但我意识到我调用 api 的路径是问题所在。 If my application name is demo , I have to call http://localhost:9090/AdminProducts to access AdminProducts, not http://localhost:9090/demo/AdminProducts .如果我的应用程序名称是demo ,我必须调用http://localhost:9090/AdminProducts来访问 AdminProducts,而不是http://localhost:9090/demo/AdminProducts This name may be use after deploying the war in tomcat.在 tomcat 中部署 war 后可能会使用此名称。

If “No message available” is accompanied by 404, just check your http request, any syntax issue might be the case.如果“没有可用的消息”伴随着 404,只需检查您的 http 请求,任何语法问题都可能是这种情况。

at least mine was a syntax error in a request while posting with postman.至少我在使用邮递员发帖时请求中出现语法错误。

我得到了同样的错误,但我意识到我正在返回 String 我将它更改为 ResponseEntity 现在它工作得很好。

all your packages should be after the main package,你所有的包都应该在主包之后,

like this com.example.demo.* (all your packages)像这个 com.example.demo.* (你所有的包)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM