使用Maven创建一个简单的SpringBoot 项目


工具:IDEA 代码编辑器

1、使用IDEA创建一个Maven项目

  

2、编辑pom.xml 

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

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

    

3 、更换Maven的镜像,改为国内镜像,以免等待下载依赖的时间过长。

创建 settings.xml 文件(通常是没有这个文件的,编辑好之后记得在IDEA中要选 override)

liuchangping-deMacBook-Pro:.m2 Ricky$ vim settings.xml
liuchangping-deMacBook-Pro:.m2 Ricky$ cat settings.xml 

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">

      
          
            alimaven  
            aliyun maven  
            http://maven.aliyun.com/nexus/content/groups/public/  
            central          
          
      

liuchangping-deMacBook-Pro:.m2 Ricky$ pwd
/Users/ricky/.m2
liuchangping-deMacBook-Pro:.m2 Ricky$ 

4 、创建Spring Boot应用类

 1 package cn.longmiss.sample;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Ch1Application {
 8     public static void main(String[] args){
 9 
10         SpringApplication.run(Ch1Application.class,args);
11     }
12 }

5、 创建Controller

 1 import org.springframework.stereotype.Controller;
 2 import org.springframework.web.bind.annotation.RequestMapping;
 3 import org.springframework.web.bind.annotation.ResponseBody;
 4 
 5 @Controller
 6 public class HelloWorldController {
 7 
 8     @RequestMapping("/say.html")
 9     public @ResponseBody String say(){
10         return "Hello Spring Boot";
11     }
12 
13 }

6、 启动Ch1Application这个类, Spring Boot会帮我加载到IDEA内置的Tomcat服务器上

7、 访问地址 http://127.0.0.1:8080/say.html