一個功能:
瀏覽器發送hello請求,服務器接收請求並處理,響應Hello World字符串;
整體目錄結構如下:
1、創建一個maven功能
1、File–New–Project
2、選擇Maven 項目,點擊 next
3、命名
4、點擊 Finish,
5、File–settings…—maven,配置maven倉庫信息
2、導入spring boot依賴
在pom.xml配置文件中添加如下依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、編寫一個主程序:啓動spring boot應用
/**
* @SpringBootApplication 來標註一個主程序類,說明這是一個spring boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring 應用啓動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4、編寫相關Controller、Service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
5、運行主程序
直接運行 HelloWorldMainApplication的main方法,啓動服務;
在瀏覽器中輸入;localhost:8080//hello
6、簡化部署
點擊"package",可將項目打成jar包
將jar包拷貝出來,cmd 中用命令: java -jar 包名 啓動服務
同樣在瀏覽器中輸入:localhost:8080//hello可進行訪問,瀏覽器展示同步驟5.