SpringBoot - [02] 第一个SpringBoot程序

  • jdk
  • maven3.6.3
  • springboot最新版
  • idea

 

如果使用官网 Spring Initializr ,则需要jdk17、21、22,并且是Springboot3.x

可以在idea创建一个简单的maven项目,然后引入相关依赖即可进行Springboot的相关开发。

 

 

一、引入依赖

<!-- 父项目 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>

<!-- maven依赖 -->
<dependencies>
    <!-- WEB依赖: Tomcat,dispatcherServlet.xml -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<!-- 属性配置 -->
<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

 

 

二、编写代码

包名:src/main/java

类名:MyApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author harley
 * @date 2024/04/10 14:58
 */
@RestController
@EnableAutoConfiguration
public class MyApplication {

    @RequestMapping("/")
    String home(){
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class,args);
    }
    
}

运行代码,在页面访问localhost:8080 即可

 

 

 

 

 

— 要养成终生学习的习惯 —

posted @ 2024-03-14 20:40  HOUHUILIN  阅读(3)  评论(0编辑  收藏  举报