0

In Thymeleaf, to trigger the index.html page (present in templates folder of resources) providing a new @RequestMapping method in the application's Main class does not work (outputs the string instead of rendering the HTML page) but creating a separate @Controller class with same @RequestMapping method is triggering the desired UI.

Method 1

The @RequestMapping method in Main class

package com.personal.boot.spring.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }

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

}

Now, a get request to /index outputs "index" as a string

Method 2

The @RequestMapping method in a separate @Controller class

package com.personal.boot.spring.helloworld.controller;

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

@Controller
public class WebController {
   @RequestMapping(value = "/index")
   public String index() {
      return "index";
   }
}

Now, a get request to /index renders the index.html page in templates folder.

Why is this the case? Please help.

Thilak
  • 45
  • 1
  • 6
  • 2
    Try to annotate `HelloworldApplication` with `@Controller` instead of `@RestController` – Denis Zavedeev Nov 09 '19 at 21:40
  • @caco3 that works. Can you please elaborate? `@RestController` does include the feature of `@Controller` right? – Thilak Nov 09 '19 at 21:47
  • I found this link explaining the difference. Thank you though.[link](https://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html) – Thilak Nov 09 '19 at 21:50
  • 1
    No problem. You may also want to check out the [Spring MVC documentation](https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-controller). From the link: "@ RestController is a composed annotation that is itself meta-annotated with @Controller and @ ResponseBody to indicate a controller whose every method inherits the type-level @ ResponseBody annotation and, therefore, **writes directly to the response body versus view resolution and rendering with an HTML template**" – Denis Zavedeev Nov 09 '19 at 21:52
  • 1
    https://stackoverflow.com/questions/25242321/difference-between-spring-controller-and-restcontroller-annotation – Ryuzaki L Nov 09 '19 at 21:58

2 Answers2

2

The answer here is that @RestController includes also @ResponseBody annotation, which forces spring to convert returned parameter explicitly

Alex Faster
  • 199
  • 10
2

The @Controller is a specialization of @Component annotation and identical to spring mvc. while @RestController is a specialization of @Controller annotation that used for supporting REST services. It is actually a convenience controller annotated with @Controller and @ResponseBody.

https://www.javacodegeeks.com/2017/08/difference-restcontroller-controller-annotation-spring-mvc-rest.html