On the way of Java calling restful API interface

Absrtact: Recently, there is a demand to provide customers with some restful API interfaces. QA uses postman for testing, but the test interface of postman is similar to that called by Java, but not the same. Therefore, he wants to write a program to test the restful API interface. Since HTTPS is used, the handling of HTTPS should also be considered. Since I also used java to call the restful interface for the first time, I have to study it. Naturally, I have consulted some materials.

Analysis: this problem is different from the call between modules. For example, I have two modules frontend and backend. Frontend provides foreground display and backend provides data support. Previously, Hession was used to register the services provided by backend as remote services, which can be directly transferred to the backend interface at the front end. However, this is no problem for a company's own project with high coupling. However, if you register this remote service for customers, it seems that it is not very good and the coupling is too high. So consider using the following methods.

Basic introduction

For the call of restful interface, the front end generally uses Ajax call, and the back end can use many methods,

Three are introduced:

1. Implementation of httpurlconnection

2. Implementation of httpclient

3. Resttemplate of spring

1、 Httpclient

Httpclient may be familiar but unfamiliar to you. You know that it can be called remotely, such as requesting a URL, and then obtaining the return status and return information in the response. However, today's topic is HTTPS, which involves the issue of certificate or user authentication.

After confirming the use of httpclient, query relevant data and find that the new version of httpclient is different from the old version and is compatible with the old version. However, the old version is not advocated to be used, and many of them have been marked as outdated methods or classes. Today, use the old version 4.2 and the latest version 4.5 3 to write code.

Old version 4.2

Certification required

In the certificate preparation phase, you choose to use certificate authentication

Skip authentication

Skip authentication was selected during the certificate preparation phase

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>