四、创建第一个springboot项目

版权声明:版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 https://blog.csdn.net/qq_35098526/article/details/87715317

简介

spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程。它采用的是建立生产就绪的应用程序观点,优先于配置的惯例。

建构准备

  • jdk 1.8 或以上
  • maven 3.0+
  • IntelliJ IDEA
    打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。
    image
    image
    image
    创建完工程,工程的目录结构如下:
    image
  • pom文件为基本的依赖管理文件
  • resouces 资源文件
  • statics 静态资源
  • 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>

创建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!";
    }
}

启动项目,在浏览器中输入: http://localhost:8080/
image
启动成功,第一个springboot项目搭建成功!

但是这个要注意一个点,现在我的controller是写在com.honghh.bootfirst下的,所以没有问题,我们将controller包放在com.honghh.controller下执行你会发现报404
image
那我们应该怎么解决呢?

注意事项

Spring Boot 正常启动后访问Controller提示404

解决办法

方法一:

以启动类的包路径作为顶层包路径,列如启动类包为com.honghh.bootfirst,那么Controller包路径就为com.honghh.bootfirst.controller。

方法二:

在启动上方添加@ComponentScan注解,此注解为指定扫描路径,例如:

@ComponentScan(basePackages = {"com.honghh.*"})   #多个不同的以逗号分割。
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);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_35098526/article/details/87715317