Voting portal

1. Introduction

The Payment Spring Boot 1.0.4.RELEASE has been released and the project is getting better and more and more developers are trying it out. Today, Brother Pang will share the experience of using Lambda in this project, hoping to be helpful to your study and work.

2. See it for what it is

Whatever it is, we should try to see it as clearly as possible.

This was not said by some famous person, but by my middle school math teacher. He told me that when I encountered a math problem, I should not immediately immerse myself in it, but first analyze the knowledge points involved in the problem, so that I could quickly understand the idea of the problem. It has served me well to this day.

Programming is the same. When you meet a requirement, analyze the process, abstract the process, and then fill in the details.

In terms of encapsulating wechat Pay, no! Encapsulate third-party calls. It must be the following process:

The abstraction of process is the main premise of our problem solving. With the flow we can abstract it in code. Here is an implementation of App payment:

public WechatResponseEntity<ObjectNode> appPay(PayParams payParams) {
    // Consumer can only consume in this way if he wants to get the result back, kind of like setter
    WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();               
    this.client().withType(WechatPayV3Type.NATIVE, payParams)
            //BiFunction is used to organize parameters
            .function(this::payFunction)
            //TODO can add a Supplier to specify the requested client
            //Consumer is used to consume results
            .consumer(wechatResponseEntity::convert)
            // Execute the request
            .request();
    return wechatResponseEntity;
}
Copy the code

It contains two lambdas, and the annotations give us a pretty clear idea of what the process looks like.

3. Lambda practice

Then we can follow the above process to further understand the details. See how Lambda can be used in a real business process.

Tissue parameters

The purpose of organizing parameters is to call third-party interfaces, and all should conform to the requirements of third-party apis. Take the payment interface of wechat Pay APP as an example, its request message is roughly like this:

If we record the interface INPUT parameter encapsulated by ourselves as INPUT, and then set the parameter of wechat APP payment interface as OUTPUT. Their transformation relationship is essentially the following Lambda abstraction:

INPUT -> OUTPUT
Copy the code

Mathematically, it is:


O U T P U T = f ( I N P U T ) OUTPUT= f(INPUT)

The corresponding Lambda Function in Java is Function.

However, in the actual development, different processing needs to be done according to the specific situation of the interface. It needs to introduce the request method (POST in the figure above) and the interface endpoint, called TYPE. We talked about function substitution method in high school, AND I think it can be used here:

If we make:


I N P U T = f ( T Y P E . I N P U T ) INPUT = f(TYPE,INPUT)

Is derived:

(TYPE,INPUT) -> OUTPUT
Copy the code

The corresponding Lambda function in Java is BiFunction

. BiFunction

can respond to different types.
,input>
,input>

You can customize Lambda interfaces based on your actual project needs.

Invoking a Third-party Interface

In Java development, you can easily invoke Http interfaces whether you choose to request from Spring Framework’s RestTemplate or reactive client WebClient or OKHttp. It can also be abstract, so whatever method you’re using it’s providing a calling tool, and we’re going to get the tool to use it, so it’s great in Java, right

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get(a);
}
Copy the code

The Lambda formula is:

() -> T
Copy the code

However, Payment Spring Boot has no intention of abstracting the calling layer at this time. If abstraction is considered, it will be implemented using Supplier

.

The results of consumption

Eventually we all need to consume the result of the call, which can only be consumed once at a macro level. The corresponding Lambda:

(T) -> {}
Copy the code

There is no return value for consuming T. Like eating an apple, after eating is not immediately a product, otherwise you have to quickly eat some 💊.

Java provides a Consumer

interface as a Lambda interface for consumption, which is easy to understand.

However, in Payment Spring Boot, the result of the request needs to be returned to the calling end, that is, the return value is required. At that time, I considered for a long time, and finally chose Consumer

between Function

and Consumer

, perhaps because it is closer to the essence of consumption.


4. To summarize

The Payment Spring Boot project has established a good iterative mechanism so far. Today’s sharing will help you understand the nature of Lambda and see through the nature of some of the requirements in the project, abstract them, and figure out their cascading relationships. Ok, today’s sharing is over here, I am: code farmer xiao Pangge, pay more attention to share the daily development dribs and DRBS.

Personal blog: felord.cn