第三节 wireMock的json的URL的匹配方式、传参方式、header和返回值

一、URL的匹配方式

1)绝对匹配带参数:http://localhost:9999/your/url?and=query

{
    "request": {
        "method": "GET",
        "url": "/your/url?and=query"
    },
    "response": {
        "status": 200,
        "body": "/your/url?and=query\n"
    }
}

2)绝对匹配不带参数:http://localhost:9999/your/urlPath

{
    "request": {
        "method": "GET",
        "urlPath": "/your/urlPath"
    },
    "response": {
        "status": 200,
        "body": " urlPath: /your/urlPath"
    }
}

3)带参数,正则匹配:http://localhost:9999/your/urlPattern/jhkj?and=query

{
    "request": {
        "method": "GET",
        "urlPattern": "/your/urlPattern/([a-z]*)\\?and=query"
    },
    "response": {
        "status": 200,
        "body": "/your/urlPattern/([a-z]*)\\?and=query"
    }
}

4)不带参数,正则匹配:http://localhost:9999/your/urlPathPattern/sdfdsf

{
    "request": {
        "method": "GET",
       "urlPathPattern": "/your/urlPathPattern/([a-z]*)"
    },
    "response": {
        "status": 200,
        "body": "/your/urlPathPattern/([a-z]*)"
    }
}

5)单独传参数:http://localhost:9999/api/products/queryParameters/NoRegular?search=chin

{
"priority": 1,
    "request": {
        "method": "GET",
        "urlPath": "/api/products/queryParameters/NoRegular",
        "queryParameters": {
            "search": {
                "contains": "chin"
            }
 	
        }
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Type": "application/json"
        },
 	"body": "{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China\" }"
    
}
}

6)单独传参数,正则参数:http://localhost:9999/api/products/queryParameters/Regular?search_term=aswiremocka

{
    "request": {
     	   "method": "GET",
      	      "url": "/your/queryParameters",
 "queryParameters" : {
     "search_term" : {
     		   "matches" : "^(.*)wiremock([A-Za-z]+)$"
     		 }
   	 }
    },
    "response": {
        "status": 200,
        "body": "dsfsdsdfsdf"
    }
}

二、POST的传参方式

1)Body方式1:

接口:http://localhost:9999/api/products/imput/body/JSON

Body:{"total_results": 4}

返回:/api/products/imput/body/JSON.

示例:

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当使用`restTemplate.exchange()`方法时,可以通过将请求的HTTP方法、URL、请求头和请求体作为递来发送HTTP请求。如果请求需要发送JSON格式的请求体,可以将请求体对象转换为JSON字符串,然后设置请求头中的`Content-Type`为`application/json`。 以下是一个使用`restTemplate.exchange()`方法发送JSON请求体的示例代码: ```java RestTemplate restTemplate = new RestTemplate(); // 创建请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // 创建请求体对象 MyRequestBody requestBody = new MyRequestBody("John", "Doe"); // 将请求体对象转换为JSON字符串 ObjectMapper mapper = new ObjectMapper(); String requestBodyJson = mapper.writeValueAsString(requestBody); // 创建请求对象 HttpEntity<String> request = new HttpEntity<>(requestBodyJson, headers); // 发送HTTP请求 String url = "https://example.com/api/users"; ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); ``` 在上面的示例中,`MyRequestBody`是一个包含两个属性的POJO类,表示请求体对象。通过`ObjectMapper`将请求体对象转换为JSON字符串,并将其设置为HTTP请求体。然后,通过设置请求头的`Content-Type`为`application/json`来告诉服务器发送的是JSON格式的请求体。最后,使用`restTemplate.exchange()`方法发送HTTP请求,并将响应体解析为字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值