温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

四、创建第一个springboot项目

发布时间:2020-08-04 00:25:40 来源:网络 阅读:1279 作者:狼興人生 栏目:编程语言

简介

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

建构准备

  • jdk 1.8 或以上
  • maven 3.0+
  • IntelliJ IDEA
    打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。
    四、创建第一个springboot项目
    四、创建第一个springboot项目
    四、创建第一个springboot项目
    创建完工程,工程的目录结构如下:
    四、创建第一个springboot项目
  • 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/
四、创建第一个springboot项目
启动成功,第一个springboot项目搭建成功!

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

注意事项

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);
    }

}

文章来源: https://blog.csdn.net/qq_35098526/article/details/87715317

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI