A text to understand how to write java interface to call

There are many computer language categories, such as C, C++, C#, Java, Php, Python, etc. They have their own characteristics and areas of expertise, but they are not all-powerful. In a slightly larger project, multiple languages ​​are used together, and how do these programming languages ​​communicate? What do you mean, for example, a method I wrote in Java, how do other programming languages ​​call it? This is the problem that this article will explore.

In general, method-level programming language communication uses a network interface form that exposes only formal parameters and results for others to call. Interfaces are generally divided into interface definers and interface callers. The definer can specify the type and return form of the received parameters, while the interface definer can only access the parameters specified by the interface definer. It is called webservice (network service).

The previous approach was to use XML as the interface format definition, and then through Http to do communication and request, such as the famous SOAP, in fact, now, but now the popular RestFul style Rest interface form, but still use XML + HTTP, then this What is the difference between the two? The biggest difference is that SOAP returns mainly in XML format. Sometimes you need to attach some auxiliary files, and Rest can also return JSON type strings, which reduces a lot of messy XML tags. This article takes Java as an example, uses her to write an interface, and let others call it, so as to simply show how the interface call is a process.

A text to understand how to write java interface to call

step:

1, this machine is equipped with Java JDK runtime environment and programming IDE (such as Myeclipse)

2. Create a maven project to download the jar package. The project structure is as follows:

A text to understand how to write java interface to call

3. The contents of the pom.xml file are as follows:

A text to understand how to write java interface to call

A text to understand how to write java interface to call

A text to understand how to write java interface to call

A text to understand how to write java interface to call

A text to understand how to write java interface to call

4, applicationContext.xml content is as follows:

A text to understand how to write java interface to call

5, web.xml is mainly configured with the container, log management and interceptor, the interceptor can achieve identity authentication before reaching the request Zheng Tao, kicking out the request without qualification, and the rest project is generally using the Jersey container. The configuration is as follows: (kendy is just my English name, you can define the path yourself)

A text to understand how to write java interface to call

6. The interceptor is as follows:

A text to understand how to write java interface to call

7, register the class to be managed by the Jersey container

A text to understand how to write java interface to call

8, interface definition (Restful style)

A text to understand how to write java interface to call

A text to understand how to write java interface to call

9, call the interface, there are many ways, this article uses HttpClient

A text to understand how to write java interface to call

10, return interface

A text to understand how to write java interface to call

11. Finally, through the Map, List and Json tools, the returned literals are processed in various ways.

A text to understand how to write java interface to call

A text to understand how to write java interface to call

Automotive Pressure Sensor

Automotive Pressure Sensor,Pressure Sensor,Air Pressure Sensor,Gas Pressure Sensor

Shenzhen Ever-smart Sensor Technology Co., LTD , https://www.fluhandy.com

This entry was posted in on