java - Rest API可以解析hashmap<string,object>吗?

标签 java json rest jersey

我想知道在使用输入参数时rest api是否可以执行以下操作:

假设我的 json 对象具有以下参数:

string name;

string adress;

hashmap<string,object> content;

以下是可发送内容的示例:

{
    "name": "AZ",
    "adress": "US",
    "content": {
        "clients": [
            {
                "client_ref":"213",
                "commands" : {
                    "subCommands": [
                        {
                            "num":"1",
                            "price":"10euro"
                        },
                        {
                            "num":"12,
                            "price":"10euro"
                        }
                    ]
                }
            },
            {
                "client_ref":"213",
                "commands" : {
                    "subCommands": [
                        {
                            "num":"1",
                            "price":"10euro"
                        },
                        {
                            "num":"12,
                            "price":"10euro"
                        }
                    ]
                }
            }
        ]
    }
}

问题是可以构建 HashMap ,其中对象本身可以有 n 个 HashMap 类型的子元素...?

(我使用 Jersey 作为休息实现)

最佳答案

假设您注册了一个 JSON 提供程序(例如 Jackson),并且您的模型类如下所示:

public class Foo {

    private String name;
    private String address;
    private Map<String, Object> content;

    // Getters and setters
}

以下资源方法:

@Path("foo")
public class Test {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response post(Foo foo) {
        ...
    }
}

可以处理如下请求:

POST /api/foo HTTP/1.1
Host: example.org
Content-Type: application/json

{
  "name": "AZ",
  "adress": "US",
  "content": {
    "clients": [
      {
        "client_ref": "213",
        "commands": {
          "subCommands": [...]
        }
      },
      {
        "client_ref": "213",
        "commands": {
          "subCommands": [...]
        }
      }
    ]
  }
}

关于java - Rest API可以解析hashmap<string,object>吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44539247/

相关文章:

java - 将 WCF 引用添加到 j2me 项目

java - 同步与锁定

java - Android 安全异常 - 没有权限 ACCESS_NETWORK_STATE

json - 从 Golang 调试 JSON 错误

entity-framework - 使用 ASP.NET Web API 和 Entity Framework 进行 API 版本控制

java - 这两种初始化实例变量的方式有什么区别?

Python 访问 S3 创建的 AWS SQS 消息

node.js - JSON.stringify 大对象优化

spring - REST API 调用 : Missing URI template variable 'productoId' for method parameter of type Long

python - 使用 Python 请求 GET 不起作用 - Web 客户端和浏览器可以工作