spring初次使用運到的定義Url但卻報500錯誤

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對象輸出指定格式的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章