0

Problem with web starter project from spring initializer. Springboot 2.2.0

I just created a spring boot 2.2.0 project from spring initializer website with web starter dependency. The application works fine if I user @RestController annotation and send request from postman but when I use @Controller annotation and try to load the hello.html page which is in /resources/templates directory it throws null or white label errors. It's weird the package structure is perfect but it's not working.

I could not find any answer thread so far for this issue.

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
KD007
  • 49
  • 2

4 Answers4

1

Possible Issue:

When you use @Controller add @ResponseBody @ResponseBody is required when we use @Controller. @RestController is a special version of @Controller in which @ResponseBody is active by default

@Controller vs @RestController


with @Controller

@Controller
public class UserController {

    @GetMapping(value= "/hello")
    public @ResponseBody String sayHello()
    {   
        retrun "Hello";
    }
}

with @RestContoller

@RestController
public class UserController {

    @GetMapping(value= "/hello")
    public String sayHello()
    {   
        retrun "Hello";
    }
}
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
0

@RestController : If it is a @RestController it just return the value whatever we given in return statement. For eg:

HomeController.java

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

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

The output is: home

It just only returns the return statement

@Controller : The @Controller annotation is returns the view page (resource/template/home.html). For Eg:

HomeController.java

 package com.example.demo;

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

 @Controller
 public class HomeController {

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

home.html

<!DOCTYPE html>
<html>
 <head>
   <meta charset="UTF-8">
   <title>Insert title here</title>
 </head>
 <body>
   <img alt="Image" src="/images/barbie.jpg">
 </body>
</html>

The output: It display home.html page

enter image description here

Ramya
  • 95
  • 1
  • 1
  • 7
  • What about view resolver, it won’t work without it. –  Jul 25 '19 at 05:29
  • It's a simple controller trying to render the corresponding view. I think some how the application is not able to find the corresponding page in the project directory. Could you provide how the view is searched in spring application? – KD007 Jul 25 '19 at 16:10
  • No html files are loaded which are in resources folder. Its weird! – KD007 Jul 25 '19 at 21:35
  • I done this using spring boot - gradle application. In Spring boot we can also use html file. – Ramya Jul 26 '19 at 09:33
0

Is thymeleaf dependency added to your project??

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
0

Simply updating the maven dependency resolved the issue. Thank you guys for your response.

KD007
  • 49
  • 2