Circular view path [mydemo]: would dispatch back to the current handler URL [/mydemo] again. Check your ViewResolver setup!


簡單創建一個springboot工程

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!--繼承spring-boot-starter-parent,要成為一個spring boot項目,首先就必須在pom.xml中繼承spring-boot-starter-parent,同時指定其版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<!-- 環境參數,在普通maven項目中,需要在pom.xml中配置插件來修改jdk版本,utf-8編碼等環境參數,在spring boot中則更加簡單。
在eclipse中按ctrl+左鍵點擊上面的spring-boot-starter-parent,查看其源碼 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 編譯字符編碼為utf-8 -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- 輸出字符編碼為UTF-8 -->
<java.version>1.8</java.version><!-- jdK版本 -->
</properties>


<dependencies>
<!--核心依賴,包括auto-configuration , logging和YAML。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!--springmvc,代表web模塊,在這個模塊中含了許多JAR包,有spring相關的jar,內置tomcat服務器,jackson等,這些web項目中常用的的功能都會自動引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--數據庫連接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

<!--Spring boot熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

</dependencies>

<!-- 編譯生成可執行jar文件,默認情況下,maven打包生成的jar文件是用來給其他項目依賴用的,是無法直接運行的。
spring boot根據自生需要,提供了一個插件來生成可執行jar文件。在spring-boot-starter-parent源碼中可以找到 -->
<build>
<!--在瀏覽器中的訪問路徑,如果將它改成helloworld,再執行maven&#45;&#45;update,這時運行項目的訪問路徑是
http://localhost:8080/helloworld/ 而不是項目名的 http://localhost:8080/test-->
<!--<finalName>demo</finalName>-->
<!-- 在自己項目的pom.xml中聲明這個插件,就會生效 -->
<plugins>
<!-- maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

啟動類
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication
/*spring boot默認會加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類,
DataSourceAutoConfiguration類使用了@Configuration注解向spring注入了dataSource bean。
因為工程中如果沒有關於dataSource相關的配置信息,當spring創建dataSource bean因缺少相關的信息就會報錯。
在Application類上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
阻止spring boot自動注入dataSource bean*/
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

控制層
@Controller
public class DemoController {

@RequestMapping("/mydemo")
public String index(){
return "mydemo";
}
}

運行項目沒有報錯
瀏覽器訪問http://localhost:8080/mydemo
報錯

Whitelabel Error Page

 
         
         
        

This application has no explicit mapping for /error, so you are seeing this as a fallback.

 
        
Mon May 13 11:19:35 CST 2019
 
        
There was an unexpected error (type=Internal Server Error, status=500).
 
        
Circular view path [mydemo]: would dispatch back to the current handler URL [/mydemo] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
 
百度找了一下
https://blog.csdn.net/universsky2015/article/details/77965402
沒有什么用
 
在控制層修改一下
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

@RequestMapping("/mydemo")
@ResponseBody
public String index(){
return "mydemo";
}
}


再次訪問沒有出錯了

@responseBody注解的作用是將controller的方法返回的對象通過適當的轉換器轉換為指定的格式之后,寫入到response對象的body區,通常用來返回JSON數據或者是XML

 數據,需要注意的呢,在使用此注解之后不會再走試圖處理器,而是直接將數據寫入到輸入流中,他的效果等同於通過response對象輸出指定格式的數據。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM