Home > Enterprise >  spring controller returns 404 but restcontroller ok
spring controller returns 404 but restcontroller ok

Time:01-22

@RequestMapping("/api/test")
@Controller
public class TestController {

    @RequestMapping("/html")
    public String retHtml() {
        return "<html></html>";
    }
}

I have a very simple Controller. I can access the api when it has the annotation of @RestController, but it returns http status of 404 when the annotation becomes @Controller.

What could be the possible reasons for this behavior?

CodePudding user response:

First, You are getting a 404 not found error because when you annotate your java class as @Controller, Spring expects a view path to be returned in controller methods but you are returning <"html"><"html"> which is not a path. Second The code syntax is wrong. Your return type does not match what you really return (<"html"><"html">). If you want to implement a Spring MVC controller just replace The return type with String and return a view path; Else, If you want to have RestApi, Annotate the class with @RestController and return a ResponseEntity object.

  • Related