4

So I am trying to follow this guide on how to serve html files with Spring: http://spring.io/guides/gs/serving-web-content/

I have the exact same folder structure with exact same files but when I run the spring boot server, my localhost:8080/greeting will only show the string greeting that is returned from the GreetingController and that's it, if I look at the page source code, there is no html in it.

I could not find any related answers about this because all the similar answers still use the .xml file driven Spring where you declare the views in a .xml file. But this guide explicitly says that no .xml needs to be used. It should just work like that.

Mapping:

@RestController
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required = false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }
}

Using @Controller throws error:

2015-02-25 14:50:14.830 ERROR 2378 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause

javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

EDIT:

Solution: a) Use @Controller instead of @RestController b) When launching the application in IntelliJ, make sure you create a Gradle task that is ran before executing the Main class.

Kaspar
  • 1,600
  • 4
  • 24
  • 46
  • read it carefully, You will find answer. – V__ Feb 25 '15 at 12:41
  • controller returns view and this is shown to you as html file – V__ Feb 25 '15 at 12:42
  • 1
    Post your mapping in the controller. Is it like the one in the article? `@RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; }` – Javi Mollá Feb 25 '15 at 12:43
  • hi, i have the same problem. it works fine if i run the project via command line, but fails when use Intellij. what does it mean to create a gradle task before run the application?\ – Haifeng Zhang May 05 '16 at 17:47
  • @HaifengZhang, I got stuck at the same place. Did you figure it out? I don't understand what gradle task to run before the application starts. – GC001 Feb 24 '17 at 21:51
  • @Chen Unfortunately I didn't figure it out :( – Haifeng Zhang Feb 27 '17 at 16:03
  • @HaifengZhang, actually I realized that you can directly access HTML pages under static/public folder without going through controllers. – GC001 Feb 28 '17 at 21:19

2 Answers2

7

You are using @RestController (the documentation) instead of @Controller.

A convenience annotation that is itself annotated with @Controller and @ResponseBody.

And @ResponseBody just return to the caller whatever the method returns, string greeting in your case.

As for the Circular view path exception you are getting, it's probably related to ViewResolvers. From the spring boot docs

There are many implementations of ViewResolver to choose from, and Spring on its own is not opinionated about which ones you should use. Spring Boot, on the other hand, installs one or two for you depending on what it finds on the classpath and in the application context

So, there is probably something missing in the dependencies. Do you have spring-boot-starter-thymeleaf dependency configured?

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • 1
    It should not matter. See: http://stackoverflow.com/questions/25242321/difference-between-spring-controller-and-restcontroller-annotation – Kaspar Feb 25 '15 at 12:46
  • Actually, that's exactly what matters, because of `@ResponseBody` – Predrag Maric Feb 25 '15 at 12:48
  • Edited my post. Using @Controller throws an error when loading the page. – Kaspar Feb 25 '15 at 12:51
  • Can provide any additional info if needed. Every solution I find on google tells to create a .xml file to solve this problem but the guide i followed says that it's not needed.. – Kaspar Feb 25 '15 at 13:07
  • @Kasper. The example uses thymeleaf for rendering and i hope you have that and can you post your main class and pom.xml or other build files? Just to make sure all are in place. – minion Feb 25 '15 at 13:13
  • @Kaspar I've edited my answer, probably something with dependecies – Predrag Maric Feb 25 '15 at 13:13
  • I think I actually solved it now. I use Intellij to work on Java, and the launcher I had created in there, caused the problem. It launched the server but errors occurred. When I ran the project manually from console with ./gradlew bootRun, everything worked as expected. – Kaspar Feb 25 '15 at 13:16
4

Have you added the following dependency in your pom.xml?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
Gilbert Lopez
  • 163
  • 1
  • 6