溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

四、創(chuàng)建第一個(gè)springboot項(xiàng)目

發(fā)布時(shí)間:2020-08-04 00:25:40 來(lái)源:網(wǎng)絡(luò) 閱讀:1295 作者:狼興人生 欄目:編程語(yǔ)言

簡(jiǎn)介

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)先于配置的慣例。

建構(gòu)準(zhǔn)備

  • jdk 1.8 或以上
  • maven 3.0+
  • IntelliJ IDEA
    打開(kāi)Idea-> new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開(kāi)啟web功能)->點(diǎn)下一步就行了。
    四、創(chuàng)建第一個(gè)springboot項(xiàng)目
    四、創(chuàng)建第一個(gè)springboot項(xiàng)目
    四、創(chuàng)建第一個(gè)springboot項(xiàng)目
    創(chuàng)建完工程,工程的目錄結(jié)構(gòu)如下:
    四、創(chuàng)建第一個(gè)springboot項(xiàng)目
  • pom文件為基本的依賴管理文件
  • resouces 資源文件
  • statics 靜態(tài)資源
  • templates 模板資源
  • application.properties 配置文件
  • SpringbootApplication程序的入口。
    POM文件源碼:
<?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>

創(chuàng)建Controller

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/
四、創(chuàng)建第一個(gè)springboot項(xiàng)目
啟動(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
四、創(chuàng)建第一個(gè)springboot項(xiàng)目
那我們應(yīng)該怎么解決呢?

注意事項(xià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

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI