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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With