利用注解 + 反射消除重复代码

本文展示了如何通过注解和反射技术优化银行API接口的代码,以消除重复并提高代码的可维护性和准确性。通过定义接口参数POJO类、接口和字段的注解,以及反射实现动态接口参数组装,将参数处理、加签和请求调用的逻辑集中在一个方法中,从而减少了代码冗余。
摘要由CSDN通过智能技术生成

假设银行提供了一些 API 接口,对参数的序列化有点特殊,不使用 JSON,而是需要我们 把参数依次拼在一起构成一个大字符串。

按照银行提供的 API 文档的顺序,把所有参数构成定长的数据,然后拼接在一起作为整 个字符串。 因为每一种参数都有固定长度,未达到长度时需要做填充处理:

字符串类型的参数不满长度部分需要以下划线右填充,也就是字符串内容靠左;

数字类型的参数不满长度部分以 0 左填充,也就是实际数字靠右;

货币类型的表示需要把金额向下舍入 2 位到分,以分为单位,作为数字类型同样进行 左填充。

对所有参数做 MD5 操作作为签名(为了方便理解,Demo 中不涉及加盐处理)。

比如,创建用户方法和支付方法的定义是这样的:

代码很容易实现,直接根据接口定义实现填充操作、加签名、请求调用操作即可:

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class BankService {

    public static String createUser(String name, String identity, String mobile, int age) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        //字符串靠左,多余的地方_填充
        stringBuilder.append(String.format("%-10s", name).replace(' ', '_'));
        //字符串靠左,多余的地方_填充
        stringBuilder.append(String.format("%-18s", identity).replace(' ', '_'));
        //数字靠右,多余的地方用0填充
        stringBuilder.append(String.format("%05d", age));
        //字符串靠左,多余的地方_填充
        stringBuilder.append(String.format("%-11s", mobile).replace(' ', '_'));
        //最后加上MD5作为签名
        stringBuilder.append(DigestUtils.md2Hex(stringBuilder.toString()));
        return Request.Post("http://localhost:8080/reflection/bank/createUser")
                .bodyString(stringBuilder.toString(), ContentType.APPLICATION_JSON)
                .execute().returnContent().asString();
    }

    public static String pay(long userId, BigDecimal amount) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        //数字靠右,多余的地方用0填充
        stringBuilder.append(String.format("%020d", userId));
        //金额向下舍入2位到分,以分为单位,作为数字靠右,多余的地方用0填充
        stringBuilder.append(String.format("%010d", amount.setScale(2, RoundingMode.DOWN).multiply(new BigDecimal("100")).longValue()));
        //最后加上MD5作为签名
        stringBuilder.append(DigestUtils.md2Hex(stringBuilder.toString()));
        return Request.Post("http://localhost:8080/reflection/bank/pay")
                .bodyString(stringBuilder.toString(), ContentType.APPLICATION_JSON)
                .execute().returnContent().asStri

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值