我想知道在使用输入参数时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/