From d526cf637048d71f046667f26e76436e9d489861 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: 星期日, 23 四月 2023 22:45:11 +0800
Subject: [PATCH] 优化代码
---
springboot/src/main/java/com/java110/boot/rest/RestApi.java | 358 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 358 insertions(+), 0 deletions(-)
diff --git a/springboot/src/main/java/com/java110/boot/rest/RestApi.java b/springboot/src/main/java/com/java110/boot/rest/RestApi.java
new file mode 100644
index 0000000..2c64774
--- /dev/null
+++ b/springboot/src/main/java/com/java110/boot/rest/RestApi.java
@@ -0,0 +1,358 @@
+package com.java110.boot.rest;
+
+import com.alibaba.fastjson.JSONObject;
+import com.java110.boot.smo.IApiServiceSMO;
+import com.java110.core.base.controller.BaseController;
+import com.java110.core.log.LoggerFactory;
+import com.java110.doc.annotation.Java110ApiDoc;
+import com.java110.doc.annotation.Java110RequestMappingDoc;
+import com.java110.doc.annotation.Java110RequestMappingsDoc;
+import com.java110.intf.user.IUserInnerServiceSMO;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.vo.ResultVo;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+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;
+
+/**
+ * rest api
+ * Created by wuxw on 2018/10/16.
+ */
+@RestController
+@RequestMapping(path = "/api")
+@Api(value = "瀵瑰缁熶竴鎻愪緵鏈嶅姟鎺ュ彛鏈嶅姟")
+@Java110ApiDoc(
+ title = "HC灏忓尯绠$悊绯荤粺api鎺ュ彛鏂囨。",
+ description = "HC灏忓尯绠$悊绯荤粺api鎺ュ彛鏂囨。",
+ company = "Java110宸ヤ綔瀹�",
+ version = "v1.5"
+)
+
+@Java110RequestMappingsDoc(
+ mappingsDocs = {
+ @Java110RequestMappingDoc(name = "鐢ㄦ埛涓績", resource = "userDoc", url = "http://127.0.0.1:8008", seq = 1, startWay = "boot"),
+ @Java110RequestMappingDoc(name = "璧勪骇涓績", resource = "communityDoc", url = "http://127.0.0.1:8008", seq = 2, startWay = "boot"),
+ @Java110RequestMappingDoc(name = "鍟嗘埛涓績", resource = "storeDoc", url = "http://127.0.0.1:8008", seq = 3, startWay = "boot"),
+ @Java110RequestMappingDoc(name = "璐︽埛涓績", resource = "acctDoc", url = "http://127.0.0.1:8008", seq = 4, startWay = "boot"),
+ @Java110RequestMappingDoc(name = "閫氱敤涓績", resource = "commonDoc", url = "http://127.0.0.1:8008", seq = 5, startWay = "boot"),
+ @Java110RequestMappingDoc(name = "璐圭敤涓績", resource = "feeDoc", url = "http://127.0.0.1:8008", seq = 6, startWay = "boot"),
+ @Java110RequestMappingDoc(name = "鎶ヨ〃涓績", resource = "reportDoc", url = "http://127.0.0.1:8008", seq = 7, startWay = "boot"),
+ }
+)
+public class RestApi extends BaseController {
+
+ private static Logger logger = LoggerFactory.getLogger(RestApi.class);
+ private static final String VERSION = "version";
+ private static final String VERSION_2 = "2.0";
+ @Autowired
+ private IApiServiceSMO apiServiceSMOImpl;
+
+ @Autowired
+ private IUserInnerServiceSMO userInnerServiceSMOImpl;
+
+
+ /**
+ * 鍋ュ悍妫�鏌� 鏈嶅姟
+ *
+ * @return
+ */
+ @RequestMapping(path = "/health", method = RequestMethod.GET)
+ @ApiOperation(value = "鏈嶅姟鍋ュ悍妫�鏌�", notes = "test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+ public String health() {
+ return "";
+ }
+
+ /**
+ * 鍋ュ悍妫�鏌� 鏈嶅姟
+ *
+ * @return
+ */
+ @RequestMapping(path = "/checkUserServiceVersion", method = RequestMethod.GET)
+ @ApiOperation(value = "妫�鏌ョ敤鏈嶅姟鐗堟湰", notes = "test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+ public String checkUserServiceVersion() {
+ return userInnerServiceSMOImpl.getUserServiceVersion("test");
+ }
+
+
+ /**
+ * 璧勬簮璇锋眰 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 = "service", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+ public ResponseEntity<String> servicePost(@PathVariable String service,
+ @RequestBody String postInfo,
+ HttpServletRequest request) {
+ ResponseEntity<String> responseEntity = null;
+ Map<String, String> headers = new HashMap<String, String>();
+ try {
+
+ this.getRequestInfo(request, headers);
+ headers.put(CommonConstant.HTTP_SERVICE, service);
+ headers.put(CommonConstant.HTTP_METHOD, CommonConstant.HTTP_METHOD_POST);
+ logger.debug("api锛歿} 璇锋眰鎶ユ枃涓猴細{},header淇℃伅涓猴細{}", service, postInfo, headers);
+ responseEntity = apiServiceSMOImpl.service(postInfo, headers);
+ } catch (Throwable e) {
+ logger.error("璇锋眰post 鏂规硶[" + service + "]澶辫触锛�" + postInfo, e);
+ responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ logger.debug("api锛歿} 杩斿洖淇℃伅涓猴細{}", service, responseEntity);
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
+ return responseEntity;
+ }
+ //褰� 鎺ュ彛鐗堟湰鍙蜂负2.0鏃� 杩斿洖閿欒澶勭悊
+ if (headers.containsKey(VERSION) && VERSION_2.equals(headers.get(VERSION))) {
+ return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, responseEntity.getBody());
+ }
+ 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;
+ Map<String, String> headers = new HashMap<String, String>();
+ try {
+ this.getRequestInfo(request, headers);
+ headers.put(CommonConstant.HTTP_SERVICE, service);
+ headers.put(CommonConstant.HTTP_METHOD, CommonConstant.HTTP_METHOD_GET);
+ logger.debug("api锛歿} 璇锋眰鎶ユ枃涓猴細{},header淇℃伅涓猴細{}", service, headers);
+ responseEntity = apiServiceSMOImpl.service(JSONObject.toJSONString(getParameterStringMap(request)), headers);
+ } catch (Throwable e) {
+ logger.error("璇锋眰get 鏂规硶[" + service + "]澶辫触锛�", e);
+ responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ logger.debug("api锛歿} 杩斿洖淇℃伅涓猴細{}", service, responseEntity);
+
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
+ return responseEntity;
+ }
+ //褰� 鎺ュ彛鐗堟湰鍙蜂负2.0鏃� 杩斿洖閿欒澶勭悊
+ if (headers.containsKey(VERSION) && VERSION_2.equals(headers.get(VERSION))) {
+ return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, responseEntity.getBody());
+ }
+
+ 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;
+ Map<String, String> headers = new HashMap<String, String>();
+ try {
+
+ this.getRequestInfo(request, headers);
+ headers.put(CommonConstant.HTTP_SERVICE, service);
+ headers.put(CommonConstant.HTTP_METHOD, CommonConstant.HTTP_METHOD_PUT);
+ logger.debug("api锛歿} 璇锋眰鎶ユ枃涓猴細{},header淇℃伅涓猴細{}", service, postInfo, headers);
+ responseEntity = apiServiceSMOImpl.service(postInfo, headers);
+ } catch (Throwable e) {
+ logger.error("璇锋眰put 鏂规硶[" + service + "]澶辫触锛�", e);
+ responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ logger.debug("api锛歿} 杩斿洖淇℃伅涓猴細{}", service, responseEntity);
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
+ return responseEntity;
+ }
+ //褰� 鎺ュ彛鐗堟湰鍙蜂负2.0鏃� 杩斿洖閿欒澶勭悊
+ if (headers.containsKey(VERSION) && VERSION_2.equals(headers.get(VERSION))) {
+ return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, responseEntity.getBody());
+ }
+
+ 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 = "service", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+ public ResponseEntity<String> serviceDelete(@PathVariable String service,
+ HttpServletRequest request) {
+ ResponseEntity<String> responseEntity = null;
+ Map<String, String> headers = new HashMap<String, String>();
+ try {
+
+ this.getRequestInfo(request, headers);
+ headers.put(CommonConstant.HTTP_SERVICE, service);
+ headers.put(CommonConstant.HTTP_METHOD, CommonConstant.HTTP_METHOD_DELETE);
+ logger.debug("api锛歿} 璇锋眰鎶ユ枃涓猴細{},header淇℃伅涓猴細{}", service, "", headers);
+
+ responseEntity = apiServiceSMOImpl.service(JSONObject.toJSONString(getParameterStringMap(request)), headers);
+ } catch (Throwable e) {
+ logger.error("璇锋眰delete 鏂规硶[" + service + "]澶辫触锛�", e);
+ responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+
+ logger.debug("api锛歿} 杩斿洖淇℃伅涓猴細{}", service, responseEntity);
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
+ return responseEntity;
+ }
+ //褰� 鎺ュ彛鐗堟湰鍙蜂负2.0鏃� 杩斿洖閿欒澶勭悊
+ if (headers.containsKey(VERSION) && VERSION_2.equals(headers.get(VERSION))) {
+ return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, responseEntity.getBody());
+ }
+ 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;
+ }
+ }
+
+
+ /**
+ * 璧勬簮璇锋眰 post鏂瑰紡
+ *
+ * @param resource 璇锋眰鎺ュ彛鏂瑰紡
+ * @param postInfo post鍐呭
+ * @param request 璇锋眰瀵硅薄 鏌ヨ澶翠俊鎭� url绛変俊鎭�
+ * @return http status 200 鎴愬姛 鍏朵粬澶辫触
+ */
+ @RequestMapping(path = "/{resource}/{action}", method = RequestMethod.POST)
+ @ApiOperation(value = "璧勬簮post璇锋眰", notes = "test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+ @ApiImplicitParam(paramType = "query", name = "subServicePost", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+ public ResponseEntity<String> subServicePost(
+ @PathVariable String resource,
+ @PathVariable String action,
+ @RequestBody String postInfo,
+ HttpServletRequest request) {
+ ResponseEntity<String> responseEntity = null;
+ Map<String, String> headers = new HashMap<String, String>();
+ try {
+ this.getRequestInfo(request, headers);
+ headers.put(CommonConstant.HTTP_SERVICE, "/" + resource + "/" + action);
+ headers.put(CommonConstant.HTTP_RESOURCE, resource);
+ headers.put(CommonConstant.HTTP_ACTION, action);
+ headers.put(CommonConstant.HTTP_METHOD, CommonConstant.HTTP_METHOD_POST);
+ logger.debug("api锛歿} 璇锋眰鎶ユ枃涓猴細{},header淇℃伅涓猴細{}", action, postInfo, headers);
+ responseEntity = apiServiceSMOImpl.service(postInfo, headers);
+ } catch (Throwable e) {
+ logger.error("璇锋眰post 鏂规硶[" + action + "]澶辫触锛�" + postInfo, e);
+ responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ logger.debug("api锛歿} 杩斿洖淇℃伅涓猴細{}", action, responseEntity);
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
+ return responseEntity;
+ }
+ //褰� 鎺ュ彛鐗堟湰鍙蜂负2.0鏃� 杩斿洖閿欒澶勭悊
+ if (headers.containsKey(VERSION) && VERSION_2.equals(headers.get(VERSION))) {
+ return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, responseEntity.getBody());
+ }
+ return responseEntity;
+ }
+
+ /**
+ * 璧勬簮璇锋眰 get鏂瑰紡
+ *
+ * @param request 璇锋眰瀵硅薄 鏌ヨ澶翠俊鎭� url绛変俊鎭�
+ * @return http status 200 鎴愬姛 鍏朵粬澶辫触
+ */
+
+ @RequestMapping(path = "/{resource}/{action}", method = RequestMethod.GET)
+ @ApiOperation(value = "璧勬簮get璇锋眰", notes = "test: 杩斿洖 2XX 琛ㄧず鏈嶅姟姝e父")
+ @ApiImplicitParam(paramType = "query", name = "subServiceGet", value = "鐢ㄦ埛缂栧彿", required = true, dataType = "String")
+ public ResponseEntity<String> subServiceGet(
+ @PathVariable String resource,
+ @PathVariable String action,
+ HttpServletRequest request) {
+ ResponseEntity<String> responseEntity = null;
+ Map<String, String> headers = new HashMap<String, String>();
+ try {
+ this.getRequestInfo(request, headers);
+ headers.put(CommonConstant.HTTP_SERVICE, "/" + resource + "/" + action);
+ headers.put(CommonConstant.HTTP_RESOURCE, resource);
+ headers.put(CommonConstant.HTTP_ACTION, action);
+ headers.put(CommonConstant.HTTP_METHOD, CommonConstant.HTTP_METHOD_GET);
+ logger.debug("api锛歿} 璇锋眰鎶ユ枃涓猴細{},header淇℃伅涓猴細{}", "", headers);
+ responseEntity = apiServiceSMOImpl.service(JSONObject.toJSONString(getParameterStringMap(request)), headers);
+ } catch (Throwable e) {
+ logger.error("璇锋眰get 鏂规硶[" + action + "]澶辫触锛�", e);
+ responseEntity = new ResponseEntity<String>("璇锋眰鍙戠敓寮傚父锛�" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ logger.debug("api锛歿} 杩斿洖淇℃伅涓猴細{}", action, responseEntity);
+
+ if (responseEntity.getStatusCode() == HttpStatus.OK) {
+ return responseEntity;
+ }
+ //褰� 鎺ュ彛鐗堟湰鍙蜂负2.0鏃� 杩斿洖閿欒澶勭悊
+ if (headers.containsKey(VERSION) && VERSION_2.equals(headers.get(VERSION))) {
+ return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, responseEntity.getBody());
+ }
+
+ return responseEntity;
+ }
+
+
+ public IApiServiceSMO getApiServiceSMOImpl() {
+ return apiServiceSMOImpl;
+ }
+
+ public void setApiServiceSMOImpl(IApiServiceSMO apiServiceSMOImpl) {
+ this.apiServiceSMOImpl = apiServiceSMOImpl;
+ }
+
+ public IUserInnerServiceSMO getUserInnerServiceSMOImpl() {
+ return userInnerServiceSMOImpl;
+ }
+
+ public void setUserInnerServiceSMOImpl(IUserInnerServiceSMO userInnerServiceSMOImpl) {
+ this.userInnerServiceSMOImpl = userInnerServiceSMOImpl;
+ }
+}
--
Gitblit v1.8.0