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

fea: add config for mvc origin_response path #788

Merged
merged 2 commits into from
Feb 27, 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
16 changes: 10 additions & 6 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.xiaomi.youpin.docean.common.MethodInvoker;
import com.xiaomi.youpin.docean.common.NamedThreadFactory;
import com.xiaomi.youpin.docean.common.Safe;
import com.xiaomi.youpin.docean.common.StringUtils;
import com.xiaomi.youpin.docean.config.HttpServerConfig;
import com.xiaomi.youpin.docean.exception.DoceanException;
import com.xiaomi.youpin.docean.listener.event.Event;
Expand Down Expand Up @@ -81,6 +82,7 @@ private void setConfig(Ioc ioc) {
this.mvcConfig.setResponseOriginalValue(Boolean.valueOf(ioc.getBean(MvcConst.RESPONSE_ORIGINAL_VALUE, MvcConst.FALSE)));
this.mvcConfig.setPoolSize(Integer.valueOf(ioc.getBean(MvcConst.MVC_POOL_SIZE, String.valueOf(MvcConst.DEFAULT_MVC_POOL_SIZE))));
this.mvcConfig.setVirtualThread(Boolean.valueOf(ioc.getBean(MvcConst.VIRTUAL_THREAD, MvcConst.TRUE)));
this.mvcConfig.setResponseOriginalPath(ioc.getBean(MvcConst.RESPONSE_ORIGINAL_PATH, ""));
ioc.publishEvent(new Event(EventType.mvcBegin, this.mvcConfig));
}

Expand Down Expand Up @@ -208,12 +210,14 @@ public void callMethod(MvcContext context, MvcRequest request, MvcResponse respo
return;
}
// get whether the configuration returns an unwrapped value
if (this.mvcConfig.isResponseOriginalValue()) {
if (data instanceof String) {
response.writeAndFlush(context, (String) data);
} else {
response.writeAndFlush(context, gson.toJson(data));
}
boolean needOriginalValue = this.mvcConfig.isResponseOriginalValue();
if (!needOriginalValue && StringUtils.isNotBlank(this.mvcConfig.getResponseOriginalPath())) {
needOriginalValue = Arrays.stream(mvcConfig.getResponseOriginalPath().split(","))
.anyMatch(i -> i.equals(method.getPath()));
}
if (needOriginalValue) {
String responseData = data instanceof String ? (String) data : gson.toJson(data);
response.writeAndFlush(context, responseData);
} else {
result.setData(data);
response.writeAndFlush(context, gson.toJson(result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public class MvcConfig implements Serializable {
*/
private boolean virtualThread;

/**
* url that need to return original data like"/test1,/test2"
*/
private String responseOriginalPath;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public abstract class MvcConst {

public static final String VIRTUAL_THREAD = "$virtual-threaad";

public static final String RESPONSE_ORIGINAL_PATH = "$response-original-path";

public static final int DEFAULT_MVC_POOL_SIZE = 200;

public static ScopedValue<MvcContext> MVC_CONTEXT = ScopedValue.newInstance();
Expand Down
Loading