@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public @interface BankAPI {
String desc() default "";
String url() default "";
}
@Retention(RetentionPolicy.RUNTIME)\
@Target(ElementType.FIELD)\
@Documented\
@Inherited\
public @interface BankAPIField {\
int order() default -1;\
int length() default -1;\
String type() default "";\
}
使用
@BankAPI(url = "/bank/createUser", desc = "创建用户接口")\
@Data\
public class CreateUserAPI extends AbstractAPI {\
@BankAPIField(order = 1, type = "S", length = 10)\
private String name;\
@BankAPIField(order = 2, type = "S", length = 18)\
private String identity;\
@BankAPIField(order = 4, type = "S", length = 11)
private String mobile;\
@BankAPIField(order = 3, type = "N", length = 5)\
private int age;\
}
解析注解,利用反射获取数据
private static String remoteCall(AbstractAPI api) throws IOException {\
BankAPI bankAPI = api.getClass().getAnnotation(BankAPI.class);\
bankAPI.url();\
StringBuilder stringBuilder = new StringBuilder();\
Arrays.stream(api.getClass().getDeclaredFields())
.filter(field -> field.isAnnotationPresent(BankAPIField.class))
.sorted(Comparator.comparingInt(a -> a.getAnnotation(BankAPIField.class).order()))
.peek(field -> field.setAccessible(true))
.forEach(field -> {\
BankAPIField bankAPIField = field.getAnnotation(BankAPIField.class);\
Object value = "";\
try {\
value = field.get(api);\
} catch (IllegalAccessException e) {\
e.printStackTrace();\
}\
switch (bankAPIField.type()) {\
case "S": {\
stringBuilder.append(String.format("%-" + bankAPIField.length() + "s", value.toString()).replace(' ', '_'));\
break;\
}\
case "N": {\
stringBuilder.append(String.format("%" + bankAPIField.length() + "s", value.toString()).replace(' ', '0'));\
break;\
}\
case "M": {\
if (!(value instanceof BigDecimal))\
throw new RuntimeException(String.format("{} 的 {} 必须是BigDecimal", api, field));\
stringBuilder.append(String.format("%0" + bankAPIField.length() + "d", ((BigDecimal) value).setScale(2, RoundingMode.DOWN).multiply(new BigDecimal("100")).longValue()));\
break;\
}\
default:\
break;\
}\
});\
stringBuilder.append(DigestUtils.md2Hex(stringBuilder.toString()));\
String param = stringBuilder.toString();\
long begin = System.currentTimeMillis();\
String result = Request.Post("http://localhost:45678/reflection" + bankAPI.url())\
.bodyString(param, ContentType.APPLICATION_JSON)\
.execute().returnContent().asString();\
log.info("调用银行API {} url:{} 参数:{} 耗时:{}ms", bankAPI.desc(), bankAPI.url(), param, System.currentTimeMillis() - begin);\
return result;\
}