<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      項目實戰(zhàn)接口開發(fā)SpringBoot

      一、springboot官方demo開發(fā)

      1. 依賴包和父: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>
      
      1. 新建 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);
          }
      }
      
      1. 運行結(jié)果

        說明:內(nèi)置了web服務(wù)器

      二、使用SpringBoot開發(fā)get方法接口

      返回cookie信息的get接口開發(fā)

      1. 新建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);
          }
      }
      
      1. com.course.server 新建MyGetMethod.java
      @RestController
      public class MyGetMethod{
          @RequestMapping(value="/getCookies",method=RequestMethod.GET)
          public String getCookies(){
              return "恭喜你獲得cookies信息成功";    
          }
      }
      
      1. Resource下新建文件:application.properties
      server.port=${port:8888}
      
      1. 啟動后訪問

      2. 獲得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 裝響應(yīng)信息得類
              Cookie cookie = new Cookie("login", "true");
              response.addCookie(cookie);
              return "恭喜你獲得cookies信息成功";
          }
      }
      

      運行:

      三、一個要求攜帶cookie信息訪問的get接口開發(fā)

      1. MyGetMethod.java 新增方法:
      @RestController
      public class MyGetMethod{
          @RequestMapping(value="/get/with/Cookies",method=RequestMethod.GET)
          public String getWithCookies(HttpServletRequest request){
              // HttpServletRequest 裝請求信息的類
              // HttpServletResponse 裝響應(yīng)信息的類
              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信息來";    
          }
      }
      
      1. Jemeter訪問
        1)加一個線程組
        2)加一個HTTP請求
        3)加一個HTTP Cookie管理器

        4)加一個查看結(jié)果樹

      四、需要攜帶參數(shù)的get請求兩種開發(fā)方式

      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;                                                                 
          }
      }
      

      結(jié)果:

      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;
      }
      

      結(jié)果:

      五、使用SpringBoot開發(fā)post方法接口

      1. 新增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 "用戶名或者密碼錯誤!";
          }
      }
      
      1. 在Jmeter中測試該接口



      六、Cookie驗證和返回用戶列表的post接口開發(fā)

      1. 新增lombok依賴
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>RELEASE</version>
          <scope>compile</scope>
      </dependency>
      
      1. 新增類 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;
      }
      
      1. 新增類 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 "參數(shù)不合法";
          }
      }
      
      1. 啟動Application.java
      2. 使用Jemeter測試接口
        1)新建線程組
        2)新增HTTP Header Manager

        3)新增HTTP Cookie Manager

        4)新增HTTP Request

        5)添加結(jié)果樹

      posted @ 2023-12-14 17:45  hqq的進階日記  閱讀(1286)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲VA成无码人在线观看天堂| 日韩av高清在线看片| 亚洲精品日韩中文字幕| 免费大片av手机看片高清| 久久精产国品一二三产品 | 好吊妞人成视频在线观看| 国产95在线 | 欧美| 久久精品人人看人人爽| 成人3D动漫一区二区三区| 国产大学生粉嫩无套流白浆| 毛葺葺老太做受视频| 国产中文字幕精品免费| 国产精自产拍久久久久久蜜| 国产精品日韩av在线播放| 中文字幕一区二区久久综合| 国产99在线 | 免费| 四虎网址| 国产亚洲一区二区三区四区| 亚洲色最新高清AV网站| 97精品人妻系列无码人妻| 久久热这里只有精品最新| 精品无码国产日韩制服丝袜| 五月综合网亚洲乱妇久久| 欧洲国产成人久久精品综合| 韩国午夜福利片在线观看| 亚洲国产日韩a在线播放| 国产在线国偷精品免费看| 欧美一区二区三区性视频| 人妻中文字幕精品一页| 国产精品久久久天天影视香蕉| xxxx丰满少妇高潮| 91精品亚洲一区二区三区| 国产精品无码a∨麻豆| 中文字幕国产精品综合| 国产熟女精品一区二区三区| 国产亚洲精久久久久久无码77777| 亚洲精品成人区在线观看| 亚洲日韩性欧美中文字幕| 宝兴县| 久久成人国产精品免费软件| 国产稚嫩高中生呻吟激情在线视频|