SpringBoot-Helloworld

     SpringBoot是建立在Spring基础之上,通过最少的Spring前期配置使应用程序尽快启动并运行。学习SpringBoot之前,预先掌握使用Spring、Maven工具。

特点:

  • 嵌入式Servlet容器,不用打包WAR
  • jar与版本自动管理
  • 应用运行跟踪与指标状况
  • .......

创建工程

       1) pom.xml文件


    4.0.0

    com.demo
    spring-boot-helloworld
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
    

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

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


     2) 创建@SpringBootApplication主程序类

package com.demo; 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 
/**
 *  @SpringBootApplication 标注主程序类,说明是SpringBoot应用
 */
@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) { 
        // Spring应用启动起来
        SpringApplication.run(HelloWorldApplication.class,args);
    }
}

    3) 定义请求

       说明:程序启动时,自动扫描加载主程序类所在包以及全部子包的组件,范围以外的组件注解不自动扫描。

package com.demo.controller; 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hi")
    public String hi(){
        return "Hello World!";
    }
}

   4)运行

        4.1 选择   HelloWorldApplication --> Run as --> Java Application

SpringBoot-Helloworld_第1张图片

     4.2 测试

                     SpringBoot-Helloworld_第2张图片

 

你可能感兴趣的:(SpringBoot-Helloworld)