12

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.

2
  • 2
    For starters, make it a Controller instead of RestController Commented Apr 6, 2017 at 19:06
  • Now it still reaches the controller but I get a 404, whether the index.html is in src/main/resources or src/main/resources/static. localhost:8090/assessment/index.html still works Commented Apr 6, 2017 at 19:12

5 Answers 5

34

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I've changed it to @Controller and now I get a 404 but the request mapping is still working. Do I need to add a view resolver bean? http:// localhost:8090/assessment/index.html still works
No thats what gives me the 404
that gives a 404. I get a 500 after adding thymeleaf to my classpath
Now I'm getting a 304, its reaching my controller
13

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>

Comments

1

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.

Comments

0

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.