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

49 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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";
    }
}
0
On

You mention you placed the index.html in your src/main/webapp but as I remember spring boot default configuration, it should be under src/main/resources/templates if it should be handled through your @Controller.

Everything in webapp is exposed "as-is" for eg. assets