From ab9b63cd51ade836a883a59bff6c419c771a9d98 Mon Sep 17 00:00:00 2001
From: wuxw7 <wuxw7@asiainfo.com>
Date: 星期六, 17 十一月 2018 00:16:02 +0800
Subject: [PATCH] api 封装完成

---
 Api/src/main/java/com/java110/api/rest/RestApi.java |  168 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 155 insertions(+), 13 deletions(-)

diff --git a/Api/src/main/java/com/java110/api/rest/RestApi.java b/Api/src/main/java/com/java110/api/rest/RestApi.java
index 5cc573b..0f3b53b 100644
--- a/Api/src/main/java/com/java110/api/rest/RestApi.java
+++ b/Api/src/main/java/com/java110/api/rest/RestApi.java
@@ -1,12 +1,19 @@
 package com.java110.api.rest;
 
+import com.java110.api.smo.IApiServiceSMO;
+import com.java110.common.constant.CommonConstant;
+import com.java110.core.base.controller.BaseController;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  *
@@ -15,25 +22,160 @@
  */
 @RestController
 
-@Api(value = "Rest Api 鐢ㄦ埛鏈嶅姟")
-@RequestMapping(path = "/rest")
-public class RestApi {
+@RequestMapping(path = "/api")
+@Api(value = "瀵瑰缁熶竴鎻愪緵鏈嶅姟鎺ュ彛鏈嶅姟")
+public class RestApi extends BaseController {
 
+
+    @Autowired
+    private IApiServiceSMO apiServiceSMOImpl;
     /**
      * 鍋ュ悍妫�鏌� 鏈嶅姟
      * @return
      */
     @RequestMapping(path = "/health",method = RequestMethod.GET)
     @ApiOperation(value="鏈嶅姟鍋ュ悍妫�鏌�", notes="test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
-    //@ApiImplicitParam(paramType="query", name = "userNumber", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "Integer")
     public String health(){
         return "";
     }
 
-    @ApiOperation(value="淇濆瓨鐢ㄦ埛淇℃伅", notes="test: res_code 涓�0000琛ㄧず鎴愬姛锛屽叾浠栬〃绀哄け璐�")
-    @ApiImplicitParam(paramType="save", name = "info", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
-    @RequestMapping(path = "/saveUser",method = RequestMethod.PUT)
-    public String saveUser(@RequestParam("info") String info){
-        return "{}";
+
+    /**
+     * 璧勬簮璇锋眰 post鏂瑰紡
+     * @param service 璇锋眰鎺ュ彛鏂瑰紡
+     * @param postInfo post鍐呭
+     * @param request 璇锋眰瀵硅薄 鏌ヨ澶翠俊鎭� url绛変俊鎭�
+     * @return http status 200 鎴愬姛 鍏朵粬澶辫触
+     */
+
+    @RequestMapping(path = "/{service:.+}",method = RequestMethod.POST )
+    @ApiOperation(value="璧勬簮post璇锋眰", notes="test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+    @ApiImplicitParam(paramType="query", name = "method", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+    public ResponseEntity<String> servicePost(@PathVariable String service,
+                                              @RequestBody String postInfo,
+                                              HttpServletRequest request){
+        ResponseEntity<String> responseEntity = null;
+        try {
+            Map<String, String> headers = new HashMap<String, String>();
+            this.getRequestInfo(request, headers);
+            headers.put(CommonConstant.HTTP_SERVICE,service);
+            headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_POST);
+            responseEntity = apiServiceSMOImpl.service(postInfo,headers);
+        }catch (Throwable e){
+            logger.error("璇锋眰post 鏂规硶["+service+"]澶辫触锛�",e);
+            return new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+
+        return responseEntity;
+    }
+
+    /**
+     * 璧勬簮璇锋眰 get鏂瑰紡
+     * @param service 璇锋眰鎺ュ彛鏂瑰紡
+     * @param request 璇锋眰瀵硅薄 鏌ヨ澶翠俊鎭� url绛変俊鎭�
+     * @return http status 200 鎴愬姛 鍏朵粬澶辫触
+     */
+
+    @RequestMapping(path = "/{service:.+}",method = RequestMethod.GET )
+    @ApiOperation(value="璧勬簮get璇锋眰", notes="test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+    @ApiImplicitParam(paramType="query", name = "service", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+    public ResponseEntity<String> serviceGet(@PathVariable String service,
+                                              HttpServletRequest request){
+        ResponseEntity<String> responseEntity = null;
+        try {
+            Map<String, String> headers = new HashMap<String, String>();
+            this.getRequestInfo(request, headers);
+            headers.put(CommonConstant.HTTP_SERVICE,service);
+            headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_GET);
+            responseEntity = apiServiceSMOImpl.service("",headers);
+        }catch (Throwable e){
+            logger.error("璇锋眰get 鏂规硶["+service+"]澶辫触锛�",e);
+            return new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+
+        return responseEntity;
+    }
+
+    /**
+     * 璧勬簮璇锋眰 put鏂瑰紡
+     * @param service 璇锋眰鎺ュ彛鏂瑰紡
+     * @param postInfo 淇敼鍐呭
+     * @param request 璇锋眰瀵硅薄 鏌ヨ澶翠俊鎭� url绛変俊鎭�
+     * @return http status 200 鎴愬姛 鍏朵粬澶辫触
+     */
+
+    @RequestMapping(path = "/{service:.+}",method = RequestMethod.PUT )
+    @ApiOperation(value="璧勬簮put璇锋眰", notes="test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+    @ApiImplicitParam(paramType="query", name = "service", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+    public ResponseEntity<String> servicePut(@PathVariable String service,
+                                             @RequestBody String postInfo,
+                                             HttpServletRequest request){
+        ResponseEntity<String> responseEntity = null;
+        try {
+            Map<String, String> headers = new HashMap<String, String>();
+            this.getRequestInfo(request, headers);
+            headers.put(CommonConstant.HTTP_SERVICE,service);
+            headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_PUT);
+            responseEntity = apiServiceSMOImpl.service(postInfo,headers);
+        }catch (Throwable e){
+            logger.error("璇锋眰put 鏂规硶["+service+"]澶辫触锛�",e);
+            return new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+
+        return responseEntity;
+    }
+
+    /**
+     * 璧勬簮璇锋眰 delete鏂瑰紡
+     * @param service 璇锋眰鎺ュ彛鏂瑰紡
+     * @param request 璇锋眰瀵硅薄 鏌ヨ澶翠俊鎭� url绛変俊鎭�
+     * @return http status 200 鎴愬姛 鍏朵粬澶辫触
+     */
+
+    @RequestMapping(path = "/{service:.+}",method = RequestMethod.DELETE )
+    @ApiOperation(value="璧勬簮delete璇锋眰", notes="test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+    @ApiImplicitParam(paramType="query", name = "method", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+    public ResponseEntity<String> serviceDelete(@PathVariable String service,
+                                             HttpServletRequest request){
+        ResponseEntity<String> responseEntity = null;
+        try {
+            Map<String, String> headers = new HashMap<String, String>();
+            this.getRequestInfo(request, headers);
+            headers.put(CommonConstant.HTTP_SERVICE,service);
+            headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_DELETE);
+            responseEntity = apiServiceSMOImpl.service("",headers);
+        }catch (Throwable e){
+            logger.error("璇锋眰delete 鏂规硶["+service+"]澶辫触锛�",e);
+            return new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+
+        return responseEntity;
+    }
+
+
+
+    /**
+     * 鑾峰彇璇锋眰淇℃伅
+     * @param request
+     * @param headers
+     * @throws RuntimeException
+     */
+    private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
+        try{
+            super.initHeadParam(request,headers);
+            super.initUrlParam(request,headers);
+        }catch (Exception e){
+            logger.error("鍔犺浇澶翠俊鎭け璐�",e);
+            throw e;
+        }
+    }
+
+
+    public IApiServiceSMO getApiServiceSMOImpl() {
+        return apiServiceSMOImpl;
+    }
+
+    public void setApiServiceSMOImpl(IApiServiceSMO apiServiceSMOImpl) {
+        this.apiServiceSMOImpl = apiServiceSMOImpl;
     }
 }

--
Gitblit v1.8.0