Spring rest controller not returning html
Asked Answered
S

5

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.

Selfexecuting answered 6/4, 2017 at 18:59 Comment(2)
For starters, make it a Controller instead of RestControllerOscillograph
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 worksSelfexecuting
B
11

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>
Bottleneck answered 12/4, 2017 at 13:38 Comment(0)
H
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.

Hunnish answered 6/4, 2017 at 19:27 Comment(4)
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 worksSelfexecuting
No thats what gives me the 404Selfexecuting
that gives a 404. I get a 500 after adding thymeleaf to my classpathSelfexecuting
Now I'm getting a 304, its reaching my controllerSelfexecuting
B
11

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>
Bottleneck answered 12/4, 2017 at 13:38 Comment(0)
F
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.

Foredate answered 6/9, 2018 at 3:22 Comment(0)
L
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

Lawful answered 26/8, 2018 at 13:30 Comment(0)
G
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>
Galenism answered 16/5, 2023 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.