Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 674
Naveen Avatar asked Dec 07 '15 13:12

Naveen


People also ask

How do I send a custom error message in REST API spring boot?

The most basic way of returning an error message from a REST API is to use the @ResponseStatus annotation. We can add the error message in the annotation's reason field. Although we can only return a generic error message, we can specify exception-specific error messages.

How do you handle exceptions in spring boot REST API?

Altogether, the most common way is to use @ExceptionHandler on methods of @ControllerAdvice classes so that the exception handling will be applied globally or to a subset of controllers. ControllerAdvice is an annotation introduced in Spring 3.2, and as the name suggests, is “Advice” for multiple controllers.

How does REST API work with spring boot?

Spring Boot is a Java framework, built on top of the Spring, used for developing web applications. It allows you to create REST APIs with minimal configurations. A few benefits of using Spring Boot for your REST APIs include: No requirement for complex XML configurations.


Video Answer


1 Answers

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.

like image 187
cahen Avatar answered Oct 05 '22 11:10

cahen