您好,登錄后才能下訂單哦!
spring boot 它的設(shè)計(jì)目的就是為例簡(jiǎn)化開(kāi)發(fā),開(kāi)啟了各種自動(dòng)裝配,你不想寫各種配置文件,引入相關(guān)的依賴就能迅速搭建起一個(gè)web工程。它采用的是建立生產(chǎn)就緒的應(yīng)用程序觀點(diǎn),優(yōu)先于配置的慣例。
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.honghh</groupId>
<artifactId>boot-first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-first</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.honghh.bootfirst.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* ClassName: HelloWordController
* Description:
*
* @author honghh
* @date 2019/02/19 15:58
*/
@RestController
public class HelloWordController {
@RequestMapping("/")
public String index() {
return "Hello Spring Boot!";
}
}
啟動(dòng)項(xiàng)目,在瀏覽器中輸入: http://localhost:8080/
啟動(dòng)成功,第一個(gè)springboot項(xiàng)目搭建成功!
但是這個(gè)要注意一個(gè)點(diǎn),現(xiàn)在我的controller是寫在com.honghh.bootfirst下的,所以沒(méi)有問(wèn)題,我們將controller包放在com.honghh.controller下執(zhí)行你會(huì)發(fā)現(xiàn)報(bào)404
那我們應(yīng)該怎么解決呢?
Spring Boot 正常啟動(dòng)后訪問(wèn)Controller提示404
以啟動(dòng)類的包路徑作為頂層包路徑,列如啟動(dòng)類包為com.honghh.bootfirst,那么Controller包路徑就為com.honghh.bootfirst.controller。
在啟動(dòng)上方添加@ComponentScan注解,此注解為指定掃描路徑,例如:
@ComponentScan(basePackages = {"com.honghh.*"}) #多個(gè)不同的以逗號(hào)分割。
package com.honghh.bootfirst;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.honghh.*"})
@SpringBootApplication
public class BootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(BootFirstApplication.class, args);
}
}
文章來(lái)源: https://blog.csdn.net/qq_35098526/article/details/87715317
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。