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.

7
  • read it carefully, You will find answer. Commented Feb 25, 2015 at 12:41
  • controller returns view and this is shown to you as html file Commented Feb 25, 2015 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"; } Commented Feb 25, 2015 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?\ Commented May 5, 2016 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. Commented Feb 24, 2017 at 21:51

2 Answers 2

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?

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

7 Comments

It should not matter. See: stackoverflow.com/questions/25242321/…
Actually, that's exactly what matters, because of @ResponseBody
Edited my post. Using @Controller throws an error when loading the page.
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..
@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.
|
4

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

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

2 Comments

Why when I serve plain .html not thymeleaf templates?
The guide you cited at spring.io/guides/gs/serving-web-content uses thymeleaf so I assumed you were using a thymeleaf template. <html xmlns:th="thymeleaf.org">

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.