Skip to content

Commit

Permalink
[enhancement] support Native Return in docean-mvc Module XiaoMi#855
Browse files Browse the repository at this point in the history
  • Loading branch information
goodjava committed Jul 3, 2024
1 parent 42a6b22 commit afc6ef0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
23 changes: 18 additions & 5 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.xiaomi.youpin.docean;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -138,6 +139,7 @@ private void registerControllerMethods(Bean bean) {
}
HttpRequestMethod hrm = new HttpRequestMethod();
hrm.setTimeout(rm.timeout());
hrm.setOriginalRes(rm.originalRes());
hrm.setPath(path);
hrm.setObj(bean.getObj());
hrm.setMethod(m);
Expand Down Expand Up @@ -252,11 +254,8 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
return;
}
// get whether the configuration returns an unwrapped value
boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue();
if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) {
needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(","))
.anyMatch(i -> i.equals(method.getPath()));
}
boolean needOriginalValue = isNeedOriginalValue(method);

if (needOriginalValue) {
String responseData = data instanceof String ? (String) data : gson.toJson(data);
response.writeAndFlush(context, responseData);
Expand All @@ -269,6 +268,7 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
context.setResponse(ex);
return;
}
ex = Throwables.getRootCause(ex);
Throwable unwrapThrowable = ExceptionUtil.unwrapThrowable(ex);
result.setCode(500);
int httpCode = 200;
Expand All @@ -284,6 +284,19 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
});
}

private boolean isNeedOriginalValue(HttpRequestMethod method) {
//优先级最高
if (method.isOriginalRes()) {
return true;
}
boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue();
if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) {
needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(","))
.anyMatch(i -> i.equals(method.getPath()));
}
return needOriginalValue;
}

private Object[] getMethodParams(MvcContext context, MvcRequest request, HttpRequestMethod method) {
Object[] params = new Object[]{null};
if (context.isWebsocket()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@

long timeout() default 2000;

//是否是原始res
boolean originalRes() default false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class HttpRequestMethod {

private long timeout;

//结果不封装
private boolean originalRes;

private Map<String, Class> genericSuperclassTypeArguments;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class MvcContext {

private Object response;

private String contentType;
private String contentType = "application/json; charset=utf-8";

/**
* rate limited or exceeded quota
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.xiaomi.youpin.docean.Mvc;
import com.xiaomi.youpin.docean.bo.MvcConfig;
import com.xiaomi.youpin.docean.common.Cons;
Expand Down

0 comments on commit afc6ef0

Please sign in to comment.