Spring controller gets invoked but returns 404

wesleyy :

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);
    }
}
Saravana :

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!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=428375&siteId=1