felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

使用 STS 创建 Sprint boot 项目 #155

Open felix-cao opened 5 years ago

felix-cao commented 5 years ago

一、下载 Spring Tool Suite

下载地址: Download, 下载解压后可以看到一个sts-bundle,里面有三个文件夹,一个法律信息,一个tc Server,一个sts

执行 STS.exe, 其实就是 eclipse,所以第一次也会提示设置工作空间,自行设置即可。

二、创建 Spring boot 项目

创建 Spring 应用的过程同 https://start.spring.io/ 中所示--实际上访问的接口一致。

2.1、 File —> New —> Spring Starter Project

2.2、输入相关信息

image

2.3、选择 web

image

点击 finish 完成

三、HelloWorld

上面的步骤完成后,我们可以看到如下目录和文件

image

HelloWorldApplication.java 改动如下:

package com.developer.helloworld;

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

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

@Controller
@SpringBootApplication
public class HelloWorldApplication {

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

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

运行,跑一把

image

日志:

image

启动浏览器打开 127.0.0.1:8080,可以看见 "Hello World!"

需要注意的是:

@SpringBootApplication
public class HelloWorldczfApplication {

    @RequestMapping("/")
    @ResponseBody
    String home() {
      return "Hello World!dfd";
    }

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

四、添加 devtools

在项目名称上点击右键 -> Spring -> Add DevTools,此时在/pom.xml中为多了一个 spring-boot-maven-plugin

Reference