欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹

 更新時(shí)間:2022年10月21日 17:06:32   作者:執(zhí)久呀  
這篇文章主要介紹了SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧

發(fā)送虛擬請(qǐng)求訪問(wèn)controller

我們?cè)趖est類中虛擬訪問(wèn)controller,就得發(fā)送虛擬請(qǐng)求。

先創(chuàng)建一個(gè)controller

package com.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tests")
public class TestController {
    @GetMapping
    public String test(){
        System.out.println("test is running");
        return "test is success";
    }
}

在test中 ,這個(gè)是一個(gè)get請(qǐng)求,所以我們調(diào)用get,如果是put,則調(diào)用put即可

package com;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開(kāi)啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest {
    @Test
//    注入虛擬MVC調(diào)用對(duì)象
    public void test(@Autowired MockMvc mvc) throws Exception {
       //創(chuàng)建虛擬請(qǐng)求,當(dāng)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
//執(zhí)行請(qǐng)求
        mvc.perform(builder);
    }
}

訪問(wèn)需要用到的一個(gè)RequestBuilder,我們按ctrl+h顯示出它的實(shí)現(xiàn)類

運(yùn)行結(jié)果

打印出了結(jié)果,說(shuō)明訪問(wèn)成功

匹配響應(yīng)執(zhí)行狀態(tài)

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開(kāi)啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest {
    @Test
//    注入虛擬MVC調(diào)用對(duì)象
    public void test(@Autowired MockMvc mvc) throws Exception {
       //創(chuàng)建虛擬請(qǐng)求,當(dāng)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
//        執(zhí)行請(qǐng)求
        ResultActions action = mvc.perform(builder);
        //設(shè)置預(yù)期值與真實(shí)值進(jìn)行比較,成功則測(cè)試通過(guò),失敗則測(cè)試不通過(guò)
        //定義本次調(diào)用的預(yù)期值
        StatusResultMatchers status= MockMvcResultMatchers.status();
        //預(yù)計(jì)本次調(diào)用成功的狀態(tài)為200
        ResultMatcher ok=status.isOk();
        //添加預(yù)計(jì)值到本次調(diào)用過(guò)程中進(jìn)行匹配
        action.andExpect(ok);
    }
}

運(yùn)行成功不會(huì)有任何反應(yīng)

當(dāng)將get改為put制造一個(gè)錯(cuò)誤,或修改不存在的路徑等其他錯(cuò)誤,則就會(huì)報(bào)出錯(cuò)誤信息。

匹配響應(yīng)體

虛擬請(qǐng)求體匹配

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開(kāi)啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest {
    @Test
//    注入虛擬MVC調(diào)用對(duì)象
    public void testBody(@Autowired MockMvc mvc) throws Exception {
        //創(chuàng)建虛擬請(qǐng)求,當(dāng)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
//        執(zhí)行請(qǐng)求
        ResultActions action = mvc.perform(builder);
        //設(shè)置預(yù)期值與真實(shí)值進(jìn)行比較,成功則測(cè)試通過(guò),失敗則測(cè)試不通過(guò)
        //定義本次調(diào)用的預(yù)期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //預(yù)計(jì)本次調(diào)用成功的狀態(tài)為200
        ResultMatcher result= content.string("test is success1");
        //添加預(yù)計(jì)值到本次調(diào)用過(guò)程中進(jìn)行匹配
        action.andExpect(result);
    }
}

如果一致則不會(huì)有任何錯(cuò)誤信息出現(xiàn), 若信息不一致,則會(huì)出現(xiàn)

匹配json格式響應(yīng)體

先創(chuàng)建一個(gè)類pojo對(duì)象

package com.pojo;
import lombok.Data;
@Data
public class Person {
    private String  name;
    private String  age;
    private String  detail;
}

controller下

package com.controller;
import com.pojo.Person;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tests")
public class TestController {
    @RequestMapping("/person")
    public Person testPerson(){
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge("14");
        person.setDetail("xijie");
        return person;
    }
}

啟動(dòng)訪問(wèn)得到一組json數(shù)據(jù)

我們?cè)跍y(cè)試類中修改一個(gè),使他產(chǎn)生錯(cuò)誤的信息

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//開(kāi)啟虛擬MVC調(diào)用
@AutoConfigureMockMvc
public class WebTest 
    @Test
//    注入虛擬MVC調(diào)用對(duì)象
    public void testJson(@Autowired MockMvc mvc) throws Exception {
        //創(chuàng)建虛擬請(qǐng)求,當(dāng)前訪問(wèn)/tests,MockMvcRequestBuilders是一個(gè)工具類
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests/person");
//        執(zhí)行請(qǐng)求
        ResultActions action = mvc.perform(builder);
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result= content.json("{\"name\":\"zhangsan\",\"age\":\"14\",\"detail\":\"xijie1\"}");
        //添加預(yù)計(jì)值到本次調(diào)用過(guò)程中進(jìn)行匹配
        action.andExpect(result);
    }
}

運(yùn)行結(jié)果

匹配響應(yīng)頭

   @Test
//    注入虛擬MVC調(diào)用對(duì)象
    public void testHeader(@Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder= MockMvcRequestBuilders.get("/tests");
        ResultActions action = mvc.perform(builder);
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        ResultMatcher result = header.string("Content-Type", "application/json");
        //添加預(yù)計(jì)值到本次調(diào)用過(guò)程中進(jìn)行匹配
       action.andExpect(result);
    }

匹配了一個(gè)/tests,返回字符串的方法。,就可以看出它的差別了

@RestController
@RequestMapping("/tests")
public class TestController {
    @GetMapping
    public String test(){
        System.out.println("test is running");
        return "test is success";
    }
}

一般的做法都是將這些寫在同一方法。

到此這篇關(guān)于SpringBoot請(qǐng)求發(fā)送與信息響應(yīng)匹配實(shí)現(xiàn)方法介紹的文章就介紹到這了,更多相關(guān)SpringBoot請(qǐng)求發(fā)送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot集成MyBatis的三種方式

    SpringBoot集成MyBatis的三種方式

    Spring Boot與MyBatis的集成為Java開(kāi)發(fā)者提供了一種簡(jiǎn)便而強(qiáng)大的方式來(lái)訪問(wèn)和操作數(shù)據(jù)庫(kù),在本文中,我們將深入解析Spring Boot集成MyBatis的多種方式,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2023-12-12
  • 解決SpringBoot加載application.properties配置文件的坑

    解決SpringBoot加載application.properties配置文件的坑

    這篇文章主要介紹了SpringBoot加載application.properties配置文件的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java httpclient設(shè)置超時(shí)時(shí)間和代理的方法

    java httpclient設(shè)置超時(shí)時(shí)間和代理的方法

    這篇文章主要介紹了java httpclient設(shè)置超時(shí)時(shí)間和代理的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • JAVA中StringBuffer與String的區(qū)別解析

    JAVA中StringBuffer與String的區(qū)別解析

    這篇文章主要介紹了JAVA中StringBuffer與String的區(qū)別解析,需要的朋友可以參考下
    2014-02-02
  • Java7之forkjoin簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java7之forkjoin簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java7引入了Fork Join的概念,來(lái)更好的支持并行運(yùn)算。接下來(lái)通過(guò)本文給大家分享Java7之forkjoin簡(jiǎn)介,感興趣的朋友一起看看吧
    2017-06-06
  • Java實(shí)現(xiàn)HDFS文件上傳下載

    Java實(shí)現(xiàn)HDFS文件上傳下載

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)HDFS文件上傳下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resource注解的不同)

    java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resourc

    本文主要介紹了java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resource注解的不同),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 解決Request.getParameter獲取不到特殊字符bug問(wèn)題

    解決Request.getParameter獲取不到特殊字符bug問(wèn)題

    這篇文章主要介紹了解決Request.getParameter獲取不到特殊字符bug問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 最新評(píng)論