【使用forward實現API介面轉發】
最後我們選擇第2種,但底層還是第1種,所以兩咱方式都是可以正常訪問的,提供給呼叫方的是第2咱形式,這樣就只要ghost xp系統下載之家使用forward做下轉發就行了,具體程式碼如下:
[java] v
@ResponseBody
@RequestMapping(value="/openapi")
public String index(HttpServletRequest request, HttpServletResponse response) {
//獲取引數
String apiName = request.getParameter("api");
String version = request.getParameter("v");
//設定預設API名稱
if (StringUtils.isEmpty(apiName)) {
apiName = "demo.hello";
}
//替換API名稱中的.號為/
apiName = StringUtils.replace(apiName, ".", "/");
if (StringUtils.isEmpty(version)) {
version = "1";
}
//轉發介面
try {
request.getRequestDispatcher("/openapi/v"+version+"/"+apiName).forward(request, response);
} catch (Exception e) {
//TODO log
//TODO return error message
}
return null;
}
這樣就ok了,原來的具體controller不用做任何改動。