上一節使用Django寫的一個接口程序,這一節講用IDEA配合Springboot創建web項目。一個是python,一個是java。
參考鏈接:http://www.uxys.com/html/JavaKfjs/20191008/840.html
一、使用IDEA創建Web項目
這個例子用的是IDEA的社區版,不過企業版更好用。
1. 首先安裝spring插件,安裝完成后會提示重啟IDE,重啟就行。
2. 創建web項目
3. 新建一個helloword.java

package com.example.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController public class helloword { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloWorld () { return "Hello World !"; } @RequestMapping(value = "users", method = RequestMethod.GET) public @ResponseBody Object users(){ List<String> userList = new ArrayList<>(); userList.add("tom"); userList.add("marry"); userList.add("jack"); System.out.println("get request, users api"); return userList; } @RequestMapping(value = "login", method = RequestMethod.POST) public @ResponseBody Object login(String name, String pwd){ Map<String, Object> map = new HashMap<>(); if("123".equals(pwd) && "jack".equals(name)){ map.put("status", 0); }else{ map.put("status", -1); } System.out.println("get request, login api"); return map; } @RequestMapping(value = "info", method = RequestMethod.GET) public @ResponseBody Object info(String name){ List<String> userList = new ArrayList<>(); userList.add(name); userList.add(name.length() + ""); System.out.println("get request, info api"); return userList; } }
4. 運行DemoApplication
5. 在瀏覽器地址欄輸入http://localhost:8080/hello,就可以看到頁面顯示Hello world!。
6. 如果出現8080端口沖突,可以修改端口號,在application.properties里面修改。
修改端口號:server.port=8088
修改上下文:server.servlet.context-path=/app
然后在運行DemoApplication,在瀏覽器地址欄輸入http://127.0.0.1:8088/app/hello。
二、 Jmeter測試上述接口