优化json传参

This commit is contained in:
yuye
2020-07-05 11:20:53 +08:00
parent 4c2d568376
commit 72fdc38b7a
11 changed files with 56 additions and 25 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<artifactId>Mars-Java</artifactId>
<groupId>com.github.yuyenews</groupId>
<version>3.0.23</version>
<version>3.0.25</version>
</parent>
<artifactId>mars-common</artifactId>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yuyenews</groupId>
<artifactId>Mars-Java</artifactId>
<version>3.0.23</version>
<version>3.0.25</version>
</parent>
<artifactId>mars-core</artifactId>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<artifactId>Mars-Java</artifactId>
<groupId>com.github.yuyenews</groupId>
<version>3.0.23</version>
<version>3.0.25</version>
</parent>
<artifactId>mars-jdbc</artifactId>
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yuyenews</groupId>
<artifactId>Mars-Java</artifactId>
<version>3.0.23</version>
<version>3.0.25</version>
</parent>
<artifactId>mars-mvc</artifactId>
@@ -69,8 +69,8 @@ public class BuildParams {
*/
private static Object getObject(Class cls, HttpMarsRequest request) throws Exception {
/* 如果是Json传参,那就直接转成Java对象返回 */
if(request.getContentType().startsWith(ParamTypeConstant.JSON)){
JSONObject paramJson = request.getJsonObject();
if(ParamTypeConstant.isJSON(request.getContentType())){
JSONObject paramJson = request.getJsonParam();
if(paramJson == null){
return null;
}
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yuyenews</groupId>
<artifactId>Mars-Java</artifactId>
<version>3.0.23</version>
<version>3.0.25</version>
</parent>
<artifactId>mars-server</artifactId>
@@ -19,4 +19,31 @@ public class ParamTypeConstant {
* formData提交
*/
public static final String FORM_DATA = "multipart/form-data";
/**
* 是否是json格式
* @param contentType 内容类型
* @return
*/
public static boolean isJSON(String contentType){
return contentType.startsWith(JSON) || contentType.equals(JSON);
}
/**
* 是否是formData格式
* @param contentType 内容类型
* @return
*/
public static boolean isFormData(String contentType){
return contentType.startsWith(FORM_DATA) || contentType.equals(FORM_DATA);
}
/**
* 是否是表单格式
* @param contentType 内容类型
* @return
*/
public static boolean isUrlEncoded(String contentType){
return contentType.startsWith(URL_ENCODED) || contentType.equals(URL_ENCODED);
}
}
@@ -50,19 +50,19 @@ public class HttpMarsRequestFactory {
/* 根据提交方式,分别处理参数 */
String contentType = marsRequest.getContentType();
if (contentType.startsWith(ParamTypeConstant.URL_ENCODED)) {
if (ParamTypeConstant.isUrlEncoded(contentType)) {
/* 正常的表单提交 */
String paramStr = getParamStr(inputStream);
marsParams = urlencoded(paramStr, marsParams, true);
} else if (contentType.startsWith(ParamTypeConstant.FORM_DATA)) {
} else if (ParamTypeConstant.isFormData(contentType)) {
/* formData提交,可以用于文件上传 */
Map<String, Object> result = formData(httpExchange, marsParams, files, contentType);
files = (Map<String, MarsFileUpLoad>) result.get(ParsingFormData.FILES_KEY);
marsParams = (Map<String, List<String>>) result.get(ParsingFormData.PARAMS_KEY);
} else if (contentType.startsWith(ParamTypeConstant.JSON)) {
} else if (ParamTypeConstant.isJSON(contentType)) {
/* RAW提交(json) */
JSONObject jsonParams = raw(inputStream);
marsRequest.setJsonObject(jsonParams);
marsRequest.setJsonParam(jsonParams);
}
}
/* 将提取出来的参数,放置到HttpMarsRequest中 */
@@ -27,7 +27,7 @@ public class HttpMarsRequest {
/**
* json参数
*/
private JSONObject jsonObject;
private JSONObject jsonParam;
/**
* 上传的文件
@@ -54,16 +54,16 @@ public class HttpMarsRequest {
* 获取json传参
* @return json参数
*/
public JSONObject getJsonObject() {
return jsonObject;
public JSONObject getJsonParam() {
return jsonParam;
}
/**
* 设置json传参
* @param jsonObject
* @param jsonParam
*/
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
public void setJsonParam(JSONObject jsonParam) {
this.jsonParam = jsonParam;
}
/**
@@ -71,14 +71,18 @@ public class HttpMarsRequest {
* @return 参数类型
*/
public String getContentType(){
if(getMethod().toUpperCase().equals("GET")){
try {
if(getMethod().toUpperCase().equals("GET")){
return "N";
}
List<String> ctList = httpExchange.getRequestHeaders().get("Content-type");
if(ctList == null || ctList.size() < 1){
return "N";
}
return ctList.get(0).trim().toLowerCase();
} catch (Exception e){
return "N";
}
List<String> ctList = httpExchange.getRequestHeaders().get("Content-type");
if(ctList == null || ctList.size() < 1){
return "N";
}
return ctList.get(0).trim().toLowerCase();
}
/**
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<artifactId>Mars-Java</artifactId>
<groupId>com.github.yuyenews</groupId>
<version>3.0.23</version>
<version>3.0.25</version>
</parent>
<artifactId>mars-starter</artifactId>
+1 -1
View File
@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.yuyenews</groupId>
<artifactId>Mars-Java</artifactId>
<version>3.0.23</version>
<version>3.0.25</version>
<packaging>pom</packaging>
<modules>
<module>mars-core</module>