Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement] support Native Return in docean-mvc Module #855 #856

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading