david 2 years ago
parent
commit
4f3c2954ae

BIN
out/artifacts/roma_api_jar/roma-api.jar


+ 8 - 7
src/main/java/com/roma/romaapi/controller/DbApiController.java

@@ -3,16 +3,15 @@ package com.roma.romaapi.controller;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.roma.romaapi.service.DbApiService;
+import com.roma.romaapi.utils.CommonUtil;
 import com.roma.romaapi.utils.CustomResponse;
 import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import java.io.IOException;
 import java.util.Map;
 
 @RestController
@@ -25,6 +24,8 @@ public class DbApiController {
     StringRedisTemplate stringRedisTemplate;
     @Autowired
     DbApiService dbApiService;
+    @Autowired
+    CommonUtil commonUtil;
     @RequestMapping("/test1")
     public Object test1() throws JsonProcessingException {
 //        // 获取redis中的值
@@ -41,10 +42,10 @@ public class DbApiController {
     }
 
     @RequestMapping("/dbapi/{code}")
-    public Map dbApi(@PathVariable("code") String sqlCode, HttpServletRequest request) {
-        Map<String, String[]> maps = request.getParameterMap();
+    public Map dbApi(@PathVariable("code") String sqlCode, HttpServletRequest request, @RequestBody String data) {
+        Map maps = commonUtil.analysisRequestParams(request, data);
         // 通过sqlId执行sql语句
-        Map<String, Object> ret = dbApiService.transferSql(sqlCode, maps);
+        Map ret = dbApiService.transferSql(sqlCode, maps);
         return CustomResponse.formatResponse(ret);
     }
 }

+ 6 - 10
src/main/java/com/roma/romaapi/service/DbApiService.java

@@ -1,5 +1,6 @@
 package com.roma.romaapi.service;
 
+import org.apache.commons.codec.binary.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.roma.romaapi.dao.DbApiDao;
@@ -13,26 +14,23 @@ public class DbApiService {
 
     @Autowired
     DbApiDao dbApiDao;
-    public Map<String, Object> transferSql(String sqlCode, Map<String, String[]> maps){
-        System.out.println("参数--------长度-----"+maps.size());
+    public Map<String, Object> transferSql(String sqlCode, Map<String, String> maps){
         Map hasSql = dbApiDao.dbQueryForMap(sqlCode);
         boolean isEmpty = hasSql.containsKey("sysErrorCode");
         if(isEmpty) {
             return  hasSql;
         }
-        // 获取全部的请求参数
-        Map bindData = new HashMap<>();
         String page = "1"; // 页数
         String perPage = "15"; // 显示多少条
-        // 组装要绑定的参数数据
-        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
+        Map bindData = new HashMap<>();
+        Object sqlBehavior = hasSql.get("sql_type");
+        for (Map.Entry<String, String> entry : maps.entrySet()) {
             String paramsKey = entry.getKey();
-            String paramsValue = entry.getValue()[0];
+            String paramsValue = entry.getValue();
             String fourBeforestring = "";
             if(paramsKey.length() >= 4) {
                 fourBeforestring = paramsKey.substring(0,4);
                 if (fourBeforestring.equals("dbp_")) {
-                    System.out.println("参数-------------------"+paramsValue);
                     bindData.put(paramsKey, paramsValue);
                 }
             }
@@ -43,8 +41,6 @@ public class DbApiService {
                 perPage = paramsValue;
             }
         }
-
-        Object sqlBehavior = hasSql.get("sql_type");
         Map ret = new HashMap<>();
         // query查询行为 update数据有变动行为
         switch (sqlBehavior.toString()) {

+ 38 - 0
src/main/java/com/roma/romaapi/utils/CommonUtil.java

@@ -1,8 +1,17 @@
 package com.roma.romaapi.utils;
 
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.RequestBody;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import static com.roma.romaapi.utils.JWTUtil.SIGN;
 
@@ -30,4 +39,33 @@ public class CommonUtil {
         String redisTokenKey = SIGN + token;
         return stringRedisTemplate.delete(redisTokenKey);
     }
+
+    public Map analysisRequestParams(HttpServletRequest request, @RequestBody String data) {
+        Map<String, String[]> maps = request.getParameterMap();
+        String requestContentType = request.getContentType(); // 请求类型
+        Map ret = new HashMap<>();
+        if (requestContentType.equals("application/x-www-form-urlencoded")) {
+            // 组装要绑定的参数数据
+            for (Map.Entry<String, String[]> entry : maps.entrySet()) {
+                String paramsKey = entry.getKey();
+                String paramsValue = entry.getValue()[0];
+                ret.put(paramsKey, paramsValue);
+            }
+        } else if (requestContentType.equals("application/json")) {
+            try {
+                ObjectMapper jacksonMapper = new ObjectMapper();
+                Map<String, String> jsonParams = jacksonMapper.readValue(data, new TypeReference<Map<String, String>>() {});
+                // 组装要绑定的参数数据
+                for (Map.Entry<String, String> entry : jsonParams.entrySet()) {
+                    String paramsKey = entry.getKey();
+                    String paramsValue = entry.getValue();
+                    ret.put(paramsKey, paramsValue);
+                }
+            }catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+
+        return  ret;
+    }
 }

+ 1 - 1
src/main/resources/application.properties

@@ -1 +1 @@
-#服务
server.port=8056
server.servlet.session.timeout=600

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.24.118.25:3306/lowcode?serverTimezone=UTC&tinyInt1isBit=false&transformedBitIsBoolean=false
spring.datasource.username = root
spring.datasource.password = mypass

#redis
spring.redis.database=0
spring.redis.password=
#spring.redis.password=name:password
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=5000
+#服务
server.port=8056
server.servlet.session.timeout=600

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.30.68.186:3306/lowcode?serverTimezone=UTC&tinyInt1isBit=false&transformedBitIsBoolean=false
spring.datasource.username = root
spring.datasource.password = mypass

#redis
spring.redis.database=0
spring.redis.password=
#spring.redis.password=name:password
spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.timeout=5000