gary69
gary69

Reputation: 4250

Spring rest controller not returning html

I'm using spring boot 1.5.2 and my spring rest controller looks like this

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

    @RequestMapping(method=RequestMethod.GET)
    public String index() {
        return "index";
    }

}

when I go to http://localhost:8090/assessment/ it reaches my controller but doesn't return my index.html, which is in a maven project under src/main/resources or src/main/resources/static. If I go to this url http://localhost:8090/assessment/index.html, it returns my index.html. I looked at this tutorial https://spring.io/guides/gs/serving-web-content/ and they use thymeleaf. Do I have to use thymeleaf or something like it for my spring rest controller to return my view?

My application class looks like this

@SpringBootApplication
@ComponentScan(basePackages={"com.pkg.*"})
public class Application {

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

When I add the thymeleaf dependency to my classpath I get this error (500 response code)

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

I guess I do need thymeleaf? I'm going to try and configure it properly now.

It works after changing my controller method to return index.html like this

@RequestMapping(method=RequestMethod.GET)
public String index() {
    return "index.html";
}

I think thymeleaf or software like it allows you to leave off the file extension, not sure though.

Upvotes: 12

Views: 33363

Answers (5)

user98139491579189124
user98139491579189124

Reputation: 11

It worked for me after going from @RestController to @Controller and adding thymeleaf. Be sure to use the correct one:

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

As I previously had this one and it did not work:

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
</dependency>

Upvotes: 0

karelp90
karelp90

Reputation: 339

Your example would be something like this:

Your Controller Method with your route "assessment"

@Controller
public class HomeController {

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

}

Your Thymeleaf template in "src/main/resources/templates/index.html"

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Hello World!</p>
</body>
</html>

Upvotes: 13

LalithK90
LalithK90

Reputation: 1028

If you try to "Building a RESTful Web Service" -> annotate your controller class with @RestController annotation if not annotate your controller class with @Controller class.

When we use spring as SpringMVC - @Controller

When we use spring as SpringRESTfull Web Service - @RestController

Use this link to read : Spring Stereo Type

Upvotes: 0

W.Man
W.Man

Reputation: 705

I solved this by removing @EnableWebMvc annotation from configuration class.

Spring MVC Auto-configuration provides static index.html support.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

Get more detail from Spring MVC Auto-configuration.

Upvotes: 1

Suresh A
Suresh A

Reputation: 1188

RestController annotation returns the json from the method not HTML or JSP. It is the combination of @Controller and @ResponseBody in one. The main purpose of @RestController is to create RESTful web services. For returning html or jsp, simply annotated the controller class with @Controller.

Upvotes: 34

Related Questions