table of Contents
Interface type
webservice interface, HTTP interface (SSL authentication), RPC interface
What is an interface
Interface generally refers to an abstraction (which can be another entity) that an entity provides itself to the outside world to separate external communication methods from internal operations so that it can be modified internally without affecting the way other external entities interact with it.
http interface type
HTTP1.0 defines three request methods: GET, POST and HEAD methods.
HTTP1.1 adds five new request methods: OPTIONS, PUT, DELETE, TRACE and CONNECT methods
method | description |
---|---|
GET | Request information on the specified page and return the entity subject |
HEAD | Ask the server for a response consistent with the GET request, but the response body will not be returned. This method can obtain the meta-information contained in the response message header without having to transmit the entire response content. |
POST | Submit data to the specified resource for processing request (such as submitting a form or uploading a file) |
PUT | Upload its latest content to the specified resource location, and the data transmitted from the client to the server replaces the content of the specified document. |
DELETE | Request the server to delete the resource identified by the Request-URI, that is, delete the specified page |
CONNECT | The HTTP/1.1 protocol is reserved for proxy servers that can change the connection to pipe mode |
OPTIONS | Returns the http request method supported by the server for a specific resource, allowing the client to view the server's performance |
TRACE | Echo the request received by the server, mainly for testing or diagnosis |
Applications
Publish interface
The SpringBoot release interface
@PostMapping
client can only request by POST, which is suitable for submitting data.
@GetMapping
client can only request by GET method, suitable for querying data
@DeleteMapping
client can only request by DELETE method, used to delete data
@PutMapping
client can only request by PUT method, used to modify data
Request interface
post请求
public static boolean doPost(String url, xxxx, xxxxx) throws ClientProtocolException, IOException {
LOGGER.info("{}" + url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("key1", xxx);
json.put("key2", xxx);
StringEntity httpEntity = new StringEntity(json.toJSONString(), StandardCharsets.UTF_8);
httpPost.setEntity((HttpEntity)httpEntity);
CloseableHttpResponse closeableHttpResponse = httpClient.execute((HttpUriRequest)httpPost);
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
if (statusCode != 200) {
LOGGER.error("{}" + statusCode);
return false;
}
LOGGER.info(");
return true;
}
get请求
public static Boolean doGet(String url, xxxxxxx) throws ClientProtocolException, IOException {
LOGGER.info("{}" + url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("key1", xxx);
json.put("key2", xxx);
json.put("key3", xxx);
StringEntity httpEntity = new StringEntity(json.toJSONString(), StandardCharsets.UTF_8);
httpPost.setEntity((HttpEntity)httpEntity);
CloseableHttpResponse closeableHttpResponse = httpClient.execute((HttpUriRequest)httpPost);
int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
if (statusCode != 200) {
LOGGER.error("{}" + statusCode);
return Boolean.valueOf(false);
}
LOGGER.info("");
return Boolean.valueOf(true);
}
JS接口请求
$.ajax({
url:"http://www.microsoft.com", //请求的url地址
dataType:"json", //返回格式为json
async:true,//请求是否异步,默认为异步,这也是ajax重要特性
data:{
"id":"value"}, //参数值
type:"POST", //请求方式 get、post
beforeSend:function(){
//请求前的处理
},
success:function(req){
//请求成功时处理
},
complete:function(){
//请求完成的处理
},
error:function(){
//请求出错处理
}
});
Debug interface
Can use tools postMan, page request
Solving common abnormal problems
413 Abnormal
The client request body is too large, causing the request capacity to be abnormal
1. The client attribute size in nginx needs to be set: [client_max_body_size 10m]
2. F5 forwarding needs to be set
3. The data can be split in the background