1.6 统一返回优化及扩展
jsonResponse
函数的扩展及优化,功能涵盖 自定义响应工厂、HATEOAS 支持 和 性能优化 等场景:
1.6.1 自定义响应工厂(ResponseFactory)
将响应逻辑封装为可复用的工厂类,支持分页、统一错误处理等场景:
// 自定义响应工厂类
class ResponseFactory(private val vertx: Vertx) {
// 基础响应构建
fun buildResponse(
ctx: RoutingContext,
statusCode: Int,
message: String,
data: Any? = null
) {
val response = JsonObject()
.put("timestamp", Instant.now().toString())
.put("status", if (statusCode in 200..299) "success" else "error")
.put("code", statusCode)
.put("message", m