我已经创建了演示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);
}
}
当我尝试使用 postman 工具以URL“http://localhost:8080/api/greetings”作为请求方法GET调用服务时,出现以下错误消息
{
"timestamp": 1449495844177,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/greetings"
}
对于每个Spring Boot应用程序,我不必在web.xml中配置Spring Dispatcher servlet。
有人可以帮我找出这里的缺失点吗?
最佳答案
您可能缺少@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
包括@ComponentScan
,它扫描其中的程序包和所有子程序包。您的 Controller 可能不在其中任何一个中。关于java - 使用Spring Boot + REST应用程序获取 “No message available”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34135205/