IntelliJ IDEA 建立一個spring boot web專案
阿新 • • 發佈:2019-01-06
1 通過Spring Initializr新建spring boot專案
選單選擇 : File => New => Project
點選Next
填寫專案資訊
點選Next選擇Web專案
2 新增控制器
選擇java目錄。新建控制控制器
package com.clevercode.biz.webservice.controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation. RequestMapping;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/index")
String index() {
return "Hello World! this is from /user/index";
}
}
package com.clevercode.biz.webservice.controller;
import org.springframework.web.bind.annotation. RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
public class OrderController {
@RequestMapping("/index")
String index() {
return "Hello World! this is from /user/index。";
}
}
3 啟動
4 頁面中輸入:http://localhost:8080/user/index
5 通過maven打包成為jar。開啟maven面板。點選package。
在target/webservice-1.0.0.jar檔案。
6 通過javar啟動jar檔案(需要把idea中啟動專案停止,因為8080埠是被佔用的)
在命令列中輸入
$ java -jar webservice-1.0.0.jar
7 java啟動jar包後,在瀏覽器中輸入http://localhost:8080/user/index 即可訪問。