1. 程式人生 > 實用技巧 >javax.servlet.ServletException: Circular view path......Check your ViewResolver setup!

javax.servlet.ServletException: Circular view path......Check your ViewResolver setup!

Controller返回資料到前端的時候出現了異常,工程:Spring Boot + Mybatis,Controller程式碼:

@Controller
public class UserController {
    
    @Autowired
    private UserMapper userService;
    
    @RequestMapping("/users")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }
}

返回的是一個User列表,但是拋了ServletException異常,大致的意思是沒有指定檢視結果,讓你檢查一下你的檢視配置,在springmvc中我們是使用viewResolver,通過在controller中return的字首來決定跳轉到相應的檢視。在Spring Boot中也可以通過這樣的方式,但是現在我想要返回的是一段Json資料,不需要檢視來接收。

解決辦法:

1.把@Controller註解換成@RestController

2.在方法上添加註解@ResponseBody

在這裡為什麼使用@RestController,和@Controller有什麼區別呢?不如看一下原始碼:

@Controller原始碼:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
​
    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * 
@return the suggested component name, if any */ String value() default ""; }

@RestController原始碼

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
​
    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * 
@return the suggested component name, if any * @since 4.0.1 */ String value() default ""; }

會發現在@RestController的註解中多了一個@ResponseBody註解,這就是區別所在,也就是說@RestController包含了@Controller控制器的功能,也包含了直接響應資料的功能。
3.如果想要用檢視去展示,應該要設定好檢視展示頁面,比如說用一個模板語言來接收返回的資料(thymeleaf或者freemarker等),也可以用jsp接收,但是SpringBoot官方是不推薦用jsp的,而是建議使用thymeleaf作為模板語言,這裡我以thymeleaf為例。

在maven工程pom中引入thymeleaf依賴:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <thymeleaf.version> 3.0.2.RELEASE </thymeleaf.version>
        <thymeleaf-layout-dialect.version> 2.1.1 </thymeleaf-layout-dialect.version>
        <tomcat.version>7.0.69</tomcat.version>
    </properties><parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath />
    </parent>
    .... // 中間省略
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

在application.properties中配置:

spring.datasource.url= jdbc:mysql://localhost:3306/test
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driverClassName= com.mysql.jdbc.Driver
  
spring.thymeleaf.mode = HTML5
​
# mybatis
mybatis.config-locations=classpath*:mybatis-config.xml
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.fisher.domain
​
server.session-timeout = 3600
server.port= 8080

定義一個user.html,路徑/src/main/resources/template, 用來展示資料:

  1. <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org" lang="zh">
    <body>
    <div>
        <h3>使用者列表</h3>
        <div>
              <table border = "1">
                 <tr>  
                    <td>ID</td>  
                    <td>Name</td>  
                    <td>Mark</td>  
                 </tr> 
                 <tr th:each="user : ${users}" >  
                     <td th:text="${user.id}"></td>  
                     <td th:text="${user.name}"></td>  
                     <td th:text="${user.mark}"></td>  
                 </tr> 
             </table>>
        </div>
    </div>
    </body>
    </html>

    這裡的Controller需要修改,改為:

    @Controller
    public class UserController {
        
        @Autowired
        private UserMapper userService;
        
        @RequestMapping("/users")
        public String getAllUsers(Model model) {
            List<User> users = userService.getAllUsers();
            model.addAttribute("users", users);
            return "user";
        }
    }

    因為這裡返回的是模板的路徑,自動跳轉至user.html頁面。啟動Spring Boot工程後,在瀏覽器中訪問localhost:8080/users,即可看到使用者列表