---
title: "企业级API网关"
author: "qingfeng825"
date: "2018-07-26T21:17:50Z"
tags: []
categories: ["企业架构"]
slug: "api"
draft: false
lastmod: "2018-07-26T21:17:50Z"
id: 2427590
url: "https://qingfeng825.iteye.com/blog/2427590"
---

API网关就是用来代替控制器层 Controller，使用API网关直接调用Service层

在Service层加 @APIMapping

http://localhost:8080/api?method=XX&param=XX

 

API store :  存储所有带有APIMapping 的类

存储的对像为 APIRunnable，

apiName, targetMethod, target,  放到一个Map里

 

Controller

1\. 接收HTTP参数

2\. 把HTTP参数字符串封装成java 对象

3\. 调用服务方法

4， 将返回结果进行Json序列化封装

 

API网关要做的事情：

1\. 验证系统参数合法性

例如： apiName 或者 method 为空，则抛一个异常

2\. 验证业务参数合法性

3\. 业务参数转换成java 对象

4\. 调用相对应的Service类的方法

5\. 拿到返回结果，封装成json

6\. 异常统一处理，封装异常，返回

 

 

执行顺序：  PIGatewayServlet===&gt;API handle ===&gt; store

 

String name=applicationConext.getBeanDefinitionNames

Class&lt;?&gt; type= applicationContext.getType(name)