0. 准备条件
- 安装JAVA JDK 1.8 并配置环境变量
- 安装IntelliJ IDEA
- 安装Maven
1. 创建项目
在IDEA中,点击File->New->Project,选择spring initializer,
点击Next,创建项目名称,下面的hawkzy,各位可以替换成自己喜欢的名字,工程名字就叫hello-world:
在左边和右边的窗口都选择Web:
一路next下去,会创建好工程,如下:
2. 修饰类:@RestController
在HelloWorldApplication类前增加修饰@RestController如下:
package com.hawkeye.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
注意上面同时引入了org.springframework.web.bind.annotation.RestController类。
3. 创建并修饰接口
写接口hi,并用@GetMappin("/hi")修饰:
package com.hawkeye.helloworld;
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
@GetMapping("/hi")
public String hi() {
return "hi, this is hawkzy's program";
}
}
4. 配置服务端信息
在resources文件夹下增加application.yml文件,配置如下:
5. 运行服务
在HelloWorldApplication.java标签页上点击右键,选择Run 'HelloWorldApplication',如下:
如果一切正常,可以看到提示:
此时打开浏览器输入http://localhost:8888/hi,可以看到如下页面:
至此,第一个程序已经完成了,恭喜大家!