I am writing a Spring Boot application. I have written a simple controller that gets invoked whenever the endpoint is hit, but it still returns status 404 and not the specified return value.
HelloController
@Controller public class MessageRequestController { @RequestMapping(method = RequestMethod.GET, value = "/hello", produces = "application/json") public String hello() { System.out.println("Hit me!"); return "Hello, you!"; } }
Now whenever I call localhost:8080/hello
, I see the console log "Hit me!"
, but "Hello, you!"
is never returned. Postman outputs:
{ "timestamp": 1516953147719, "status": 404, "error": "Not Found", "message": "No message available", "path": "/hello" }
Application.java
@SpringBootApplication @ComponentScan({"com.sergialmar.wschat"}) // this is the root package of everything @EntityScan("com.sergialmar.wschat") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
We went through the two most common reasons for receiving a 404 response from our Spring application. The first was using an incorrect URI while making the request. The second was mapping the DispatcherServlet to the wrong url-pattern in web. xml.
The 404 error code is configured properly, but it will caused the “. htm” extension handling conflict between the “servlet container” and Spring's “DispatcherServlet“. To solve it, try change the 404. htm to other file extension, for example 404.
Change your method return a ResponseEntity<T>
@RequestMapping(method = RequestMethod.GET, value = "/hello", produces = "application/json") public ResponseEntity<String> hello() { System.out.println("Hit me!"); return new ResponseEntity<String>("Hello, you!", HttpStatus.OK); }
or change the controller to RestController
@RestController public class MessageRequestController {...}
CURL
ubuntu:~$ curl -X GET localhost:8080/hello Hello, you!
Short version:
Annotate your endpoint method with ResponseBody to bind the return value to the response body.
@Controller public class MessageRequestController { @RequestMapping(method = RequestMethod.GET, value = "/hello", produces = "application/json") @ResponseBody public String hello() { System.out.println("Hit me!"); return "Hello, you!"; } }
You can instead annotate your class with RestController instead of Controller
to apply ResponseBody
to each method of the class.
@RestController public class MessageRequestController { @RequestMapping(method = RequestMethod.GET, value = "/hello", produces = "application/json") public String hello() { System.out.println("Hit me!"); return "Hello, you!"; } }
With @Controller, you use the default model-view from Spring Web MVC, and you're actually telling spring to render the view called Hello, you!.tml
from your resources directory (src/main/resources/templates
for a Spring Boot project, if I remember correctly).
You can read this article for more information about the Spring MVC REST Workflow.
Once you're more familiar with those concepts, you can even further customize your endpoint method using ResponseEntity.
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