当前位置 博文首页 > 文章内容

    SpringBoot-02-第一个程序

    作者: 栏目:未分类 时间:2020-09-19 11:00:36

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    • 新建一个SpringBoot项目,选中Web依赖

    • 创建好的SpringBoot项目目录结构如下:

    • 新建SpringBoot项目中的主启动类和项目依赖管理文件

      • 主启动类:SpringbootdemoApplication

        @SpringBootApplication
        public class SpringbootdemoApplication {

           public static void main(String[] args) {
               SpringApplication.run(SpringbootdemoApplication.class, args);
          }
        }
      • 项目依赖管理文件: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.3.0.RELEASE</version>
               <relativePath/> <!-- lookup parent from repository -->
           </parent>
           <groupId>com.hmx</groupId>
           <artifactId>springbootdemo</artifactId>
           <version>0.0.1-SNAPSHOT</version>
           <name>springbootdemo</name>
           <description>Demo project for Spring Boot</description>

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

           <dependencies>
               <!--web模块依赖启动器-->
               <dependency>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-web</artifactId>
               </dependency>

               <!--test类依赖-->
               <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>

           <!--Maven打包工具依赖-->
           <build>
               <plugins>
                   <plugin>
                       <groupId>org.springframework.boot</groupId>
                       <artifactId>spring-boot-maven-plugin</artifactId>
                   </plugin>
               </plugins>
           </build>
        </project>
    • 创建一个用于Web访问的Controller:HelloController.java

      @RestController
      public class HelloController {

         @RequestMapping("/hello")
         public String Hello(){
             return "Hello,SpringBoot!";
        }
      }
    • 运行并访问:http://localhost:8080/hello 端口,页面输出:Hello,SpringBoot!

      Spring Boot 入门程序完成!