- 1. How to mock an interface
- 2. How to set mock request and return in detail
- 3. Summary of one picture flow:
1. How to mock an interface
postman operation
Seeing this interface is a simple configuration completed~
Here we can call the mock service we configured before, and after clicking send, we can see the response body we configured
code call interface
I am using the SpringCloud @FeignClient
call here
@FeignClient(url = "https://ea6d6f75-9537-4701-ad88-54d1de82f4ba.mock.pstmn.io", name = "demo")
public interface DemoClient {
@GetMapping("/my/path1")
Map<String, Object> func();
}
但是!执行报错:
feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [***] and content type [text/html;charset=utf-8]
Caused by: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [***] and content type [text/html;charset=utf-8]
Probably the translation is that the default content type of the previous mock service is [text/html;charset=utf-8]. That is, it is regarded as a string of html, and only String can be returned. And the map I want to use here to receive it, or a pojo class to receive it, cannot be converted from a string, but needs a json.
So how to make the mock service return a json object? Postman provides methods under the Collections tab. Enter the details below:
2. How to set mock request and return in detail
In this interface, you can modify the request and response of the previously configured mock service.
Previously, the body of responses was simply configured on the mock service interface. In this interface, not only the previously configured body can be modified, but also the headers of the request and response can be modified.
In the response header, add a configuration Content-Type
: application/json
, save it.
Call it again in the code, and it can be received normally