如何在服务器端使用 java 从客户端接收和解析来自 HTTP POST 请求的 JSON 对象

How to receive and parse a JSON object from a HTTP POST request from client using java on the server side

我正在开发一个 iOS 应用程序,服务器端需要从客户端接收 POST。客户端在 swift 中编程,HTTP 请求使用 Alamofire 库发送。连接清晰,每次发送POST请求时服务器自动返回status code 200。 POST请求的内容也是正确的。但是如何在服务器端接收和解析客户端从 POST 请求发送的 JSON 对象呢?服务器上的某些操作需要来自 POST 请求的数据。

服务器端是用Java编写的,我在Amazon EC2服务器上使用windows服务器2019。有明确的指示就好了!

对于java应用程序,我建议您首先使用spring初始化创建一个maven和spring引导项目,其中包含spring mvc框架。 其次用 post 方法编写一个 spring mvc 控制器,它服务于来自 IOS 的请求。这是我的例子:

    @PostMapping("/xxx")
    public XXX addXXX(@Valid @RequestBody XXXAddForm xxxAddForm) {
        return xxxService.addXXX(xxxAddForm);
    }

    // this is the json object, which matches the format of your client side 
    public class XXXAddForm {
    }