1.使用idea搭建spring boot項目工程,選擇Spring Initializr,選中jdk然後next
2.輸入Group,Artifact,選中Maven Project,然後next
3.選擇web->web 然後next,再finish
4.搭建後項目目錄如下
DemoApplication.java爲項目啓動入口
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在DemoApplication.java所在包或所在包的子包下創建HelloController.java
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String hello(){
return "hello world!";
}
}
運行項目,起來後在瀏覽器輸入如下地址即可訪問。