创建第一个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了。

发布了322 篇原创文章 · 获赞 61 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/wan_ide/article/details/104921735