Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring controller gets invoked but returns 404

Tags:

java

spring

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);     } } 
like image 450
wesleyy Avatar asked Jan 26 '18 08:01

wesleyy


People also ask

Why does spring boot say 404 error?

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.

How does Spring MVC handle 404 error?

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.


2 Answers

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! 
like image 109
Saravana Avatar answered Sep 23 '22 08:09

Saravana


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.

like image 31
Marc Tarin Avatar answered Sep 22 '22 08:09

Marc Tarin