目錄
一、springboot官方demo開發
- 依賴包和父:pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.14</version>
</dependency>
</dependencies>
- 新建 SampleController.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home(){
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(SampleController.class,args);
}
}
- 執行結果
說明:內建了web伺服器
二、使用SpringBoot開發get方法介面
返回cookie資訊的get介面開發
- 新建Application.java 入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.course.server")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- com.course.server 新建MyGetMethod.java
@RestController
public class MyGetMethod{
@RequestMapping(value="/getCookies",method=RequestMethod.GET)
public String getCookies(){
return "恭喜你獲得cookies資訊成功";
}
}
- Resource下新建檔案:application.properties
server.port=${port:8888}
-
啟動後訪問
-
獲得cookies
修改com.course.server.MyGetMethod.java 程式碼:
package com.course.server;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
public class MyGetMethod {
@RequestMapping(value = "/getCookies",method= RequestMethod.GET)
public String getCookies(HttpServletResponse response){
// HttpServletRequest 裝請求資訊得類
// HttpServletResponse 裝響應資訊得類
Cookie cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "恭喜你獲得cookies資訊成功";
}
}
執行:
三、一個要求攜帶cookie資訊訪問的get介面開發
- MyGetMethod.java 新增方法:
@RestController
public class MyGetMethod{
@RequestMapping(value="/get/with/Cookies",method=RequestMethod.GET)
public String getWithCookies(HttpServletRequest request){
// HttpServletRequest 裝請求資訊的類
// HttpServletResponse 裝響應資訊的類
Cookie[] cookies = request.getCookies();
if(Objects.isNull(cookies)){
return "你必須攜帶cookies資訊來";
}
for(Cookie cookie:cookies){
if(cookie.getName().equals("login") &&
cookie.getValue().equals("true")){
return "恭喜你訪問成功!";
}
}
return "你必須攜帶cookies資訊來";
}
}
- Jemeter訪問
1)加一個執行緒組
2)加一個HTTP請求
3)加一個HTTP Cookie管理器
4)加一個檢視結果樹
四、需要攜帶引數的get請求兩種開發方式
4.1 方式1:url:key=value&key=value
@RestController
public class MyGetMethod{
@RequestMapping(value="/get/with/param",method=RequestMethod.GET)
public Map<String,Integer> getList(@RequestParam Integer start,
@RequestParam Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",500);
myList.put("衣服",200);
myList.put("乾脆面",1);
return myList;
}
}
結果:
4.2 方式2:url:ip:port/get/with/param/10/20
@RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)
public Map<String,Integer> getList(@RequestParam(required = false) Integer start,
@RequestParam(required = false) Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",500);
myList.put("衣服",200);
myList.put("乾脆面",1);
return myList;
}
結果:
五、使用SpringBoot開發post方法介面
- 新增MyPostMethod.java
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
@RestController
@RequestMapping("/v1")
public class MyPostMethod{
// 這個變數用來裝我們的cookies資訊
private static Cookie cookie;
// 使用者登入成功獲取到cookies,然後再訪問其他介面獲取到列表
@RequestMapping(value="/login",method=RequestMethod.POST)
@ApiOperation(value="登陸介面,成功後獲取cookies資訊",httpMethod="POST")
public String login(HttpServletResponse response,
@RequestParam(value="userName",required=true) String userName,
@RequestParam(value="password",required=true) String password){
if(userName.equals("zhangsan")&&password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登入成功了!";
}
return "使用者名稱或者密碼錯誤!";
}
}
- 在Jmeter中測試該介面
六、Cookie驗證和返回使用者列表的post介面開發
- 新增lombok依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
- 新增類 com/course/bean/User.java
package com.course.bean;
import lombok.Data;
@Data
public class User {
private String userName;
private String password;
private String name;
private String age;
private String sex;
}
- 新增類 com/course/server/MyPostMethod.java
package com.course.server;
import com.course.bean.User;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/v1")
public class MyPostMethod {
// 這個變數用來裝我們的cookies資訊
private static Cookie cookie;
// 使用者登入成功獲取到cookies,然後再訪問其他介面獲取到列表
@RequestMapping(value="/login",method= RequestMethod.POST)
public String login(HttpServletResponse response,
@RequestParam(value="userName",required=true) String userName,
@RequestParam(value="password",required=true) String password){
if(userName.equals("zhangsan")&&password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登入成功了!";
}
return "使用者名稱或者密碼錯誤!";
}
@RequestMapping(value="/getUserList",method = RequestMethod.POST)
public String getUserList(HttpServletRequest request,
@RequestBody User u){
// 獲取cookies
Cookie[] cookies = request.getCookies();
// 驗證cookies是否合法
for (Cookie c:cookies){
if (c.getName().equals("login") && c.getValue().equals("true") && u.getUserName().equals("zhangsan") && u.getPassword().equals("123456")){
User user = new User();
user.setName("lisi");
user.setAge("14");
user.setSex("man");
return user.toString();
}
}
return "引數不合法";
}
}
- 啟動Application.java
- 使用Jemeter測試介面
1)新建執行緒組
2)新增HTTP Header Manager
3)新增HTTP Cookie Manager
4)新增HTTP Request
5)新增結果樹