當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot菜鸟示例——Hello World
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot菜鸟示例——Hello World
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建項目
Next->Create
等待下載依賴完成
添加新類->一個HelloWord Controller類
鼠標放上自動提醒所依賴的庫,可直接添加
編寫Hello Word請求的Mapping
package cn.hiyj.idle.helloworld.controller;import org.springframework.web.bind.annotation.*;import java.util.Map;//@Controller與@ResponseBody的結合體,可返回字符串而不是返回一個“網頁鏈接” @RestController //指定url的請求可以分發到指定的類、函數 @RequestMapping("hello") public class HelloWordController {// 響應Get請求,只與GetMapping注解參數有關,與函數名無關,沒有/開頭的參數是指與類的相對路徑,相對路徑的無下級路徑就是自己// http://localhost:8888/hello@GetMappingpublic String hello() {return "OK";}// 參數添加在請求路徑中,例如http://localhost:8888/hello/WindSnowLi@GetMapping(value = "{name}")// 請求路徑的name的值傳遞給name變量,@PathVariable路徑中的變量public String name(@PathVariable String name) {return name;}// 參數添加在請求參數中,例如http://localhost:8888/hello/table?key=1&value=2中問號之后@GetMapping(value = "table")//請求參數的值傳遞給一個Map,@RequestParam注解的作用public String map(@RequestParam Map<String, String> table) {return table.toString();} }設置服務監聽的端口號
運行啟動服務
CMD curl命令測試
總結
以上是生活随笔為你收集整理的Spring Boot菜鸟示例——Hello World的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度之星比赛规则
- 下一篇: spring菜鸟总结