创建第一个SpringBoot项目——HelloSpringBoot

撒子是SpringBoot?

SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架。Spring Boot 以约定大于配置的核心思想,默认帮我们进行了很多设置,多数 Spring Boot 应用只需要很少的 Spring 配置。

创建SpringBoot项目

New->Project->Spring Initializr,选用默认的创建方式

设置项目的坐标

选择web项目

设置项目名称和路径然后Finish就好了,这样一个Spring Boot的项目就建好了。

以后我们所建的包都是在HellospringbootApplication的同级目录下。

  • HellospringbootApplication是程序的入口
  • application.properties是配置文件
  • HellospringbootApplicationTests是测试类

pom.xml中的依赖(建项目时自动导入的):

<?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 https://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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lyr</groupId>
    <artifactId>hellospringboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hellospringboot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--web依赖:tomcat,dispatcherServlet,xml..-->
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <!--打jar包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在hellospringboot包下新建一个controller包,在controller包下新建一个helloController类

package com.lyr.hellospringboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello,SpringBoot!";
    }
}

结果:

这样一个简单的helloSpringBoot就写好了!

 

可以在IDEA右侧maven中找到package打包选项就可以把项目打成jar包。

打包成功就可以在IDEA工作空间我们的项目中的target找到此jar包

在dos命令行运行该jar包,也可以访问页面了

访问http://localhost:8080/hello

打成jar包在命令行运行就不用依赖IDEA了。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建一个SpringBoot项目有两种方式。第一种方式是通过官网生成SpringBoot项目,然后导入IDEA中。具体步骤如下:首先,在官网上创建一个SpringBoot项目。然后,将项目导入到IDEA中。\[1\]第二种方式是直接通过IDEA生成SpringBoot项目。具体步骤如下:在IDEA中创建一个SpringBoot项目,然后对maven进行配置。\[2\]在配置maven时,需要点击右侧的"Maven",然后点击右上方的小扳手。接下来,选择需要引入的maven依赖,最后点击"Finish"即可完成项目创建。\[3\]总体来说,创建一个SpringBoot项目是比较简单的,希望这些步骤可以帮助到你。 #### 引用[.reference_title] - *1* [创建一个SpringBoot项目(IDEA版本,保姆级教程)](https://blog.csdn.net/weixin_42753193/article/details/123363255)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [如何创建一个SpringBoot项目](https://blog.csdn.net/qq_41134710/article/details/126273226)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值