请求日志: 显示响应体 + 新增 ShowFullUrl/ShowRunTime 配置(默认true)
This commit is contained in:
@@ -16,6 +16,8 @@ package simapi.helpers
|
||||
import std.collection.*
|
||||
import std.io.*
|
||||
import std.reflect.*
|
||||
import stdx.encoding.json.*
|
||||
import stdx.serialization.serialization.*
|
||||
import soulsoft_web_http.*
|
||||
import soulsoft_web_mvc.annotations.*
|
||||
import soulsoft_web_mvc.core.*
|
||||
@@ -304,7 +306,12 @@ struct SimApiActionInvoker {
|
||||
if (let Some(status) <- details.status) {
|
||||
context.response.statusCode = UInt16(status)
|
||||
}
|
||||
context.response.writeAsJson(details)
|
||||
// 与 soulsoft writeAsJson 的 Serializable<T> 分支一致:serialize().toJson().toString()
|
||||
// (toJson 为 DataModel 接口扩展,需先上转);writeAsJson 内部会设置 contentType,此处补上
|
||||
context.response.contentType = "application/json; charset=utf-8"
|
||||
let dm: DataModel = details.serialize()
|
||||
let jsonText = dm.toJson().toString()
|
||||
SimApiResponseWriter.write(context, jsonText)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +329,7 @@ struct SimApiActionInvoker {
|
||||
if (let s: String <- actionResult) {
|
||||
// String 原样输出文本(对齐 C# string 返回直接写入)
|
||||
context.response.contentType = "application/json; charset=utf-8"
|
||||
context.response.write(s)
|
||||
SimApiResponseWriter.write(context, s)
|
||||
} else {
|
||||
ObjectResult<Any>(actionResult).invoke(context)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package simapi.helpers
|
||||
|
||||
import soulsoft_web_http.*
|
||||
import simapi_serialization.*
|
||||
import simapi.communications.*
|
||||
|
||||
/**
|
||||
@@ -24,7 +25,8 @@ public class SimApiResponseFilter {
|
||||
context =>
|
||||
next(context)
|
||||
if (!context.response.hasStarted) {
|
||||
context.response.writeAsJson(SimApiBaseResponse())
|
||||
context.response.contentType = "application/json; charset=utf-8"
|
||||
SimApiResponseWriter.write(context, JsonSerializer.Serialize(SimApiBaseResponse()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
||||
* 移植自 C# 项目 SimApi(E:\simcu\simapi-net),遵循 MIT 许可证。
|
||||
*/
|
||||
|
||||
package simapi.helpers
|
||||
|
||||
import soulsoft_web_http.*
|
||||
|
||||
/**
|
||||
* 响应写出工具:写出响应体并缓存文本,供请求日志中间件读取。
|
||||
*
|
||||
* 背景:soulsoft 的 HttpResponse.body 为只写流(read 抛 UnsupportedException),
|
||||
* 无法像 C# 那样用 MemoryStream 替换 Body 捕获响应内容;
|
||||
* 故在统一写出入口缓存文本,请求日志中间件直接从 context.items 读取。
|
||||
*/
|
||||
public class SimApiResponseWriter {
|
||||
private init() {}
|
||||
|
||||
/**
|
||||
* 响应体缓存键(写入 HttpContext.items)。
|
||||
*/
|
||||
public static let responseBodyCacheKey = "SimApi:ResponseBodyCache"
|
||||
|
||||
/**
|
||||
* 写出响应文本并缓存(供请求日志中间件读取)。
|
||||
* @param context HTTP 上下文。
|
||||
* @param text 响应正文文本。
|
||||
*/
|
||||
public static func write(context: HttpContext, text: String): Unit {
|
||||
context.items[responseBodyCacheKey] = text
|
||||
context.response.write(text)
|
||||
}
|
||||
}
|
||||
@@ -42,9 +42,9 @@ public class SimApiResultWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/// simapi_serialization 序列化后直接写响应体
|
||||
/// simapi_serialization 序列化后直接写响应体(经 SimApiResponseWriter 缓存,供请求日志读取)
|
||||
private static func writeJson(context: HttpContext, obj: Any): Unit {
|
||||
context.response.contentType = "application/json; charset=utf-8"
|
||||
context.response.write(JsonSerializer.Serialize(obj))
|
||||
SimApiResponseWriter.write(context, JsonSerializer.Serialize(obj))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user