mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-08 21:06:40 +10:00
优化json传参
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.mars.mvc.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.core.enums.DataType;
|
||||
import com.mars.common.util.StringUtil;
|
||||
import com.mars.iserver.constant.ParamTypeConstant;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.HttpMarsResponse;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
@@ -66,7 +68,16 @@ public class BuildParams {
|
||||
* @throws Exception
|
||||
*/
|
||||
private static Object getObject(Class cls, HttpMarsRequest request) throws Exception {
|
||||
/* 如果参数类型既不是request,也不是response,那么就当做一个对象来处理 */
|
||||
/* 如果是Json传参,那就直接转成Java对象返回 */
|
||||
if(request.getContentType().startsWith(ParamTypeConstant.JSON)){
|
||||
JSONObject paramJson = request.getJsonObject();
|
||||
if(paramJson == null){
|
||||
return null;
|
||||
}
|
||||
return paramJson.toJavaObject(cls);
|
||||
}
|
||||
|
||||
/* 如果不是Json传参,那就用反射来处理 */
|
||||
Object obj = cls.getDeclaredConstructor().newInstance();
|
||||
Field[] fields = cls.getDeclaredFields();
|
||||
for(Field f : fields){
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.mars.iserver.constant;
|
||||
|
||||
/**
|
||||
* 参数传递方式
|
||||
*/
|
||||
public class ParamTypeConstant {
|
||||
|
||||
/**
|
||||
* 常规表单提交
|
||||
*/
|
||||
public static final String URL_ENCODED = "application/x-www-form-urlencoded";
|
||||
|
||||
/**
|
||||
* json提交
|
||||
*/
|
||||
public static final String JSON = "application/json";
|
||||
|
||||
/**
|
||||
* formData提交
|
||||
*/
|
||||
public static final String FORM_DATA = "multipart/form-data";
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public class RequestExecute {
|
||||
String uri = RequestUtil.getUriName(request);
|
||||
if(!PathAccess.hasAccess(uri)){
|
||||
/* 获取请求的数据,并填充表单 */
|
||||
request = HttpMarsRequestFactory.getHttpMarsRequest(httpExchange, request);
|
||||
request = HttpMarsRequestFactory.getHttpMarsRequest(request);
|
||||
|
||||
/* 通过反射执行核心控制器 */
|
||||
Class<?> cls = CoreServletClass.getCls();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.mars.iserver.par;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.mars.common.constant.MarsConstant;
|
||||
import com.mars.iserver.constant.ParamTypeConstant;
|
||||
import com.mars.iserver.par.formdata.ParsingFormData;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
@@ -28,15 +28,15 @@ public class HttpMarsRequestFactory {
|
||||
|
||||
/**
|
||||
* 从httpExchange中提取出所有的参数,并放置到HttpMarsRequest中
|
||||
* @param httpExchange 请求
|
||||
* @param marsRequest mars请求
|
||||
* @return 加工后的mars请求
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public static HttpMarsRequest getHttpMarsRequest(HttpExchange httpExchange, HttpMarsRequest marsRequest) throws Exception {
|
||||
public static HttpMarsRequest getHttpMarsRequest(HttpMarsRequest marsRequest) throws Exception {
|
||||
Map<String, MarsFileUpLoad> files = new HashMap<>();
|
||||
Map<String, List<String>> marsParams = new HashMap<>();
|
||||
|
||||
HttpExchange httpExchange = marsRequest.getHttpExchange();
|
||||
if (httpExchange.getRequestMethod().toUpperCase().equals("GET")) {
|
||||
/* 从get请求中获取参数 */
|
||||
String paramStr = httpExchange.getRequestURI().getQuery();
|
||||
@@ -49,19 +49,20 @@ public class HttpMarsRequestFactory {
|
||||
}
|
||||
|
||||
/* 根据提交方式,分别处理参数 */
|
||||
String contentType = getContentType(httpExchange);
|
||||
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
||||
String contentType = marsRequest.getContentType();
|
||||
if (contentType.startsWith(ParamTypeConstant.URL_ENCODED)) {
|
||||
/* 正常的表单提交 */
|
||||
String paramStr = getParamStr(inputStream);
|
||||
marsParams = urlencoded(paramStr, marsParams, true);
|
||||
} else if (contentType.startsWith("multipart/form-data")) {
|
||||
} else if (contentType.startsWith(ParamTypeConstant.FORM_DATA)) {
|
||||
/* 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("application/json")) {
|
||||
} else if (contentType.startsWith(ParamTypeConstant.JSON)) {
|
||||
/* RAW提交(json) */
|
||||
marsParams = raw(inputStream, marsParams);
|
||||
JSONObject jsonParams = raw(inputStream);
|
||||
marsRequest.setJsonObject(jsonParams);
|
||||
}
|
||||
}
|
||||
/* 将提取出来的参数,放置到HttpMarsRequest中 */
|
||||
@@ -71,18 +72,7 @@ public class HttpMarsRequestFactory {
|
||||
return marsRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取提交方式
|
||||
* @param httpExchange 请求对象
|
||||
* @return 提交方式
|
||||
*/
|
||||
private static String getContentType(HttpExchange httpExchange){
|
||||
List<String> ctList = httpExchange.getRequestHeaders().get("Content-type");
|
||||
if(ctList == null || ctList.size() < 1){
|
||||
return null;
|
||||
}
|
||||
return ctList.get(0).trim().toLowerCase();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从输入流里面读取所有的数据
|
||||
@@ -141,37 +131,17 @@ public class HttpMarsRequestFactory {
|
||||
/**
|
||||
* RAW提交处理
|
||||
* @param inputStream 输入流
|
||||
* @param marsParams httpMarsRequest的参数对象
|
||||
* @return httpMarsRequest的参数对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static Map<String,List<String>> raw(InputStream inputStream,Map<String,List<String>> marsParams) throws Exception {
|
||||
private static JSONObject raw(InputStream inputStream) throws Exception {
|
||||
String paramStr = getParamStr(inputStream);
|
||||
if(paramStr == null || paramStr.trim().equals("")){
|
||||
return marsParams;
|
||||
return null;
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(paramStr);
|
||||
List<String> values = null;
|
||||
for(String key : jsonObject.keySet()){
|
||||
values = marsParams.get(key);
|
||||
if(values == null){
|
||||
values = new ArrayList<>();
|
||||
}
|
||||
Object val = jsonObject.get(key);
|
||||
if(val != null){
|
||||
if(val instanceof JSONArray) {
|
||||
JSONArray jsonArray = (JSONArray)val;
|
||||
for(int i = 0; i<jsonArray.size(); i++){
|
||||
values.add(jsonArray.getString(i));
|
||||
}
|
||||
} else {
|
||||
values.add(val.toString());
|
||||
}
|
||||
}
|
||||
marsParams.put(key,values);
|
||||
}
|
||||
return marsParams;
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.mars.server.server.request;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
@@ -23,6 +24,11 @@ public class HttpMarsRequest {
|
||||
*/
|
||||
private Map<String,List<String>> marsParams;
|
||||
|
||||
/**
|
||||
* json参数
|
||||
*/
|
||||
private JSONObject jsonObject;
|
||||
|
||||
/**
|
||||
* 上传的文件
|
||||
*/
|
||||
@@ -44,6 +50,37 @@ public class HttpMarsRequest {
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取json传参
|
||||
* @return json参数
|
||||
*/
|
||||
public JSONObject getJsonObject() {
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置json传参
|
||||
* @param jsonObject
|
||||
*/
|
||||
public void setJsonObject(JSONObject jsonObject) {
|
||||
this.jsonObject = jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数类型
|
||||
* @return 参数类型
|
||||
*/
|
||||
public String getContentType(){
|
||||
if(getMethod().toUpperCase().equals("GET")){
|
||||
return "N";
|
||||
}
|
||||
List<String> ctList = httpExchange.getRequestHeaders().get("Content-type");
|
||||
if(ctList == null || ctList.size() < 1){
|
||||
return null;
|
||||
}
|
||||
return ctList.get(0).trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
* @param params
|
||||
|
||||
Reference in New Issue
Block a user