SpringBoot(一)入门

说明

本篇针对小白,从0到1搭建一个boot运行环境

前置条件

本地已经安装好jdk8、maven3.4及以上的版本、IDEA开发工具

步骤

新建工程

打开IDEA,新建bootStart工程,菜单:File->New->Project


新建boot工程导航截图.png

其余步骤按照提示操作即可!

添加pom依赖




    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
        
    
    org.example
    bootStart
    1.0-SNAPSHOT

    bootStart
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


添加启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

添加controller

package org.example;

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

@RestController
public class StartController {

    @RequestMapping("/hello")
    public String hello() {
        return "{\"key\":\"tom\",\"value\":\"tom\"}";
    }
}

运行项目

执行StartApplication的main方法,观察控制台有启动信息,待出现如下信息时,标明启动成功

......
2021-11-03 21:24:36.497  INFO 66169 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 786 ms
2021-11-03 21:24:36.864  INFO 66169 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-03 21:24:36.879  INFO 66169 --- [           main] org.example.StartApplication             : Started StartApplication in 1.643 seconds (JVM running for 2.172)

测试

在浏览器中输入: http://localhost:8080/hello 回车出现

{"key":"tom","value":"tom"}

标明服务可用

至此一个简单的服务启动成功。

你可能感兴趣的:(SpringBoot(一)入门)