开放API demo 对外开放SDK
api请求类型类注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiEvent {
String[] value() default {"BASE"};
}
API接口service缓存
@Component
public class KxbApiServiceFactory {
private static final Logger logger = LoggerFactory.getLogger(KxbApiServiceFactory.class);
/**
* 消息service注册表
*/
private Map<String, KxbApiService> apiServiceMap;
@PostConstruct
private void init() {
apiServiceMap = new HashMap<>();
initMessageService();
}
/**
* 初始化service注册表
*/
private void initMessageService() {
Map<String, KxbApiService> result = SpringBeanUtil.getApplicationContext().getBeansOfType(KxbApiService.class);
if (CollectionUtils.isEmpty(result)) {
logger.warn("初始化获取失败,没有发现策略实现 bean。");
throw new RuntimeException("初始化消息处理类失败");
}
for (Map.Entry<String, KxbApiService> entry : result.entrySet()) {
String beanName = entry.getKey();
KxbApiService value = entry.getValue();
ApiEvent dataAnnotation = value.getClass().getAnnotation(ApiEvent.class);
if (null != dataAnnotation) {
for (String string : dataAnnotation.value()) {
apiServiceMap.put(string, value);
}
} else {
logger.info("初始化,operateType is null, bean:{}", beanName);
}
}
}
/**
* Description: api service
* @Param: [operateType]
* @Return: com.example.demo.service.KxbApiService
*/
public KxbApiService obtainApiService(String operateType) {
if (operateType == null) {
logger.error("[KxbApiServiceFactory.obtainApiService] the input param is null, return [null].");
return null;
}
return apiServiceMap.get(operateType);
}
}
定义客户端接口
public interface KxbClient {
<T extends KxbResponse> T execute(KxbRequest<T> req) throws Exception;
}
客户端实现类
public class AccessTokenKxbClient implements KxbClient {
@Override
public <T extends KxbResponse> T execute(KxbRequest<T> req) throws Exception {
KxbApiServiceFactory serviceFactory = SpringBeanUtil.getBean("kxbApiServiceFactory");
return serviceFactory.obtainApiService(req.getApiMethodName()).execute(req);
}
}
请求接口
public interface KxbRequest<T extends KxbResponse> {
String getApiMethodName();
}
回复抽象类
@Data
public abstract class KxbResponse implements Serializable {
public static final int SUCCESS_VALUE = 1;
public static final String SUCCESS_MSG = "成功";
private int result;
private String errorMsg;
}
请求抽象类
@Data
public abstract class AbstractKxbRequest<T extends KxbResponse> implements KxbRequest<T> {
protected Map<String, String> headerParamsMap;
}
请求子抽象类
@Data
public abstract class AccessTokenKxbRequestSupport<T extends KxbResponse> extends AbstractKxbRequest<T> {
private String accessToken;
}
订单接口请求实类
@Data
public class OpenOrderListGetRequest extends AccessTokenKxbRequestSupport<OpenOrderListGetResponse>{
private Long orderId;
private Long kwaiItemId;
private Long relItemId;
private Integer status;
private Integer pageNumber;
private Integer pageSize;
public String getApiMethodName() {
return "open.order.list.get";
}
}
订单回复实类
@Data
public class OpenOrderListGetResponse extends KxbResponse {
private Map<String, Object> data;
}
产品请求实类
@Data
public class OpenProductListGetRequest extends AccessTokenKxbRequestSupport<OpenProductListGetResponse>{
private Long kwaiItemId;
private Long relItemId;
private Integer itemStatus;
private Integer itemType;
private Integer pageNumber;
private Integer pageSize;
@Override
public String getApiMethodName() {
return "open.item.list.get";
}
}
商品回复实类
@Data
public class OpenProductListGetResponse extends KxbResponse {
private Map<String, Object> data;
}
请求接口
public interface KxbApiService {
public <T extends KxbResponse> T execute(KxbRequest<T> req);
}
订单查询实现
@ApiEvent({"open.order.list.get"})
@Service
public class KxbApiOrderService implements KxbApiService {
@Override
public <T extends KxbResponse> T execute(KxbRequest<T> req) {
return null;
}
}
接口调用 代码示例
public class ReqTest {
public static void main(String[] args) {
// 订单
OpenProductListGetRequest openProductListGetRequest = new OpenProductListGetRequest();
KxbClient kxbClient = new AccessTokenKxbClient();
try {
OpenProductListGetResponse response = kxbClient.execute(openProductListGetRequest);
} catch (Exception e) {
e.printStackTrace();
}
// 商品
OpenOrderListGetRequest openOrderListGetRequest = new OpenOrderListGetRequest();
try {
OpenOrderListGetResponse orderResponse = kxbClient.execute(openOrderListGetRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
}