Search code examples
javaspringwebrequest-mapping

My "/" requestMapping sent me a WhiteLabel "/" error


I want to simply map my home page with a Spring Controller like this :

package com.douineau.testspringboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class HomeController {

    @GetMapping
//  @ResponseBody
    public String home() {
        return "index";
    }
}

I also have index.html in src/main/webapp folder. But the application does not recognize the mapping, whereas if comment this all, the app recognizes that it is my home page.

What am I missing?


Solution

  • You can change @Controller to @RestController. Your code should be like this.

    @RestController
    public class HomeController {
    
    @RequestMapping(value = "/", method = RequestMethod.GET)
      public String home() {
            return "index";
        }
    }
    

    or

    @RestController
    @RequestMapping(value = "/")
    public class HomeController {
    
     @GetMapping("")
      public String home() {
            return "index";
        }
    }