The front-end and back-end separation development of Android ios app based on HTTP mock (RAP)

question:

1. Front-end development needs to wait for the back-end

2. http mock is inconvenient to use

3. There are already many interfaces in the project. It takes work to mock these interfaces.

 

analyze:

1. The front-end and back-end separation seems to be more convenient to mock a solution

2. If you can find a mock that is very convenient to use

3. The implemented interface goes to our code, and the unimplemented interface goes to mock. The combination of the two can be compatible.

 

solve:

1&2. After investigation, I found that Alimama's RAP is very convenient to use. There are abundant documents , videos and examples , which can be used according to the documents.

2. The implemented interface goes to our code, and the unimplemented interface goes to mock. The solution is to report 404 when requesting an unimplemented interface. Then we can handle 404 in a unified method. In this method parse the requested url, and then use the url to splicing the interface address of the corresponding RAP. Then use tools such as URL and HTTPClient to request the URL of RAP, obtain the mock data generated by RAP, and then return the mock data to the front end.

 

The specific process is divided into two steps:

a) Unified processing of unmatched requests. There are probably several ways . We are using SpringMVC, so I added a method (let's call him the mockMapping method for now) to match requests with the /app request prefix /**/*, so that all requests that are not exactly matched will match this method to

b) In the mockMapping method, request the RAP interface through apache HTTPClient ( see the code here )

 

Probably the screenshot looks like this


 

Created mock, RAP will generate a unique corresponding url for you

This url is the part in the red box in the upper right corner of the figure below, plus the request address on the left

It should be noted that there is a part of the url in the upper right corner called mockjs, which needs to be replaced with mockjsdata , so that the data you request through httpClient is the JSON string parsed through mockjs



 
 

 --------------------------------------------------------------------

It took less than half an hour (the code was written well) to read the source code of RAP. The mock part is roughly divided into three steps to achieve

1. When you create a MOCK request, it saves the MOCK definition of the request and the corresponding generated url.

2. When requesting, we request /mockjsdata, followed by the url generated in the previous step

3. RAP passes the mockjsdata to /mock/createMockjsData.action through URLReWrite for processing. In this action, the corresponding mock definition is found through the url, and then parsed and returned

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611805&siteId=291194637