使用 Jackson 映射不同的 HTTP JSON 响应

发布于 2025-01-11 13:07:29 字数 1835 浏览 0 评论 0原文

我正在使用 Spring(Kotlin) 制作一个 Web 服务器,并尝试使用 Jackson 映射 JSON 响应。 我需要向其他API服务器发送请求,所以我决定使用OkHttp发送请求。 但是,存在一个问题,如果其他 API 服务器响应类似 4XX 的错误,Jackson 无法映射响应,因为响应具有不同的 JSON 结构。

代码如下:

@PostMapping("/test")
fun test(@org.springframework.web.bind.annotation.RequestBody requestJSON: RequestJSON): Message {
    val JSON: MediaType = "application/json; charset=utf-8".toMediaType()
    val mapper = ObjectMapper()
    val requestJSONString: String = mapper.writeValueAsString(requestJSON)

    val client = OkHttpClient()
    val body: RequestBody = requestJSONString.toRequestBody(JSON)
    val request: Request = Request.Builder()
        .header("clientid", "blahblah")
        .header("secret", "blahblah")
        .url("blahblah")
        .post(body)
        .build()
    val response: Response = client.newCall(request).execute()
    return mapper.readValue(response.body?.string(), Message::class.java)
}

“Message”类具有如下结构:

class Message {
    var message = NestedMessage()

    class NestedMessage {
        @JsonProperty("@type")
        var type: String? = null

        @JsonProperty("@service")
        var service: String? = null

        @JsonProperty("@version")
        var version: String? = null

        @JsonProperty("result")
        var resultObject = Result()

        class Result {
            var srcLangType: String? = null
            var tarLangType: String? = null
            var translatedText: String? = null
            var engineType: String? = null
            var pivot: String? = null
            var dict: String? = null
            var tarDict: String? = null
        }
    }
}

并且,其他 API 服务器的错误具有如下结构:

class Error {
    var errCode: String? = null
    var errMessage: String? = null
}

如何映射不同的 JSON 响应?

I'm making a web server using Spring(Kotlin), and trying to map JSON response using Jackson.
I need to send a request to other API server, so I decided to use OkHttp to send request.
But, there is a problem that If the other API server responds with error like 4XX, Jackson couldn't map response because the response has different JSON structure.

Here's the code:

@PostMapping("/test")
fun test(@org.springframework.web.bind.annotation.RequestBody requestJSON: RequestJSON): Message {
    val JSON: MediaType = "application/json; charset=utf-8".toMediaType()
    val mapper = ObjectMapper()
    val requestJSONString: String = mapper.writeValueAsString(requestJSON)

    val client = OkHttpClient()
    val body: RequestBody = requestJSONString.toRequestBody(JSON)
    val request: Request = Request.Builder()
        .header("clientid", "blahblah")
        .header("secret", "blahblah")
        .url("blahblah")
        .post(body)
        .build()
    val response: Response = client.newCall(request).execute()
    return mapper.readValue(response.body?.string(), Message::class.java)
}

The class "Message" has a structure like this:

class Message {
    var message = NestedMessage()

    class NestedMessage {
        @JsonProperty("@type")
        var type: String? = null

        @JsonProperty("@service")
        var service: String? = null

        @JsonProperty("@version")
        var version: String? = null

        @JsonProperty("result")
        var resultObject = Result()

        class Result {
            var srcLangType: String? = null
            var tarLangType: String? = null
            var translatedText: String? = null
            var engineType: String? = null
            var pivot: String? = null
            var dict: String? = null
            var tarDict: String? = null
        }
    }
}

And, an error of other API server has a structure like this:

class Error {
    var errCode: String? = null
    var errMessage: String? = null
}

How can I map different JSON response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2025-01-18 13:07:29

您应该为北向 API 和南向 API 创建两个不同的 Struct。

对于南向 API,请在 SouthBoundMessage 中添加 errCodeerrMessage。或者如果可以的话检查 HTTP 状态代码,然后使用不同的 Jackson 映射器来获取错误消息。

然后将 SouthBoundMessage 转换为 NorthBoundMessage,这就是您的业务逻辑。

为北向API消费者返回NorthBoundMessage和相应的HTTP代码。

You should create two different Struct for the northbound API and the southbound API.

For the southbound API, add errCode and errMessage to SouthBoundMessage. Or check the HTTP status code if you can, then use a different Jackson mapper for Error Message.

Then convert SouthBoundMessage to NorthBoundMessage, which is your business logic.

Return NorthBoundMessage and corresponding HTTP code for northbound API consumers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文