我正在创建一个休息端点,其RequestBody将是一个JSON对象:
data: () => ({
subscription: [ ... ]
value: 10
...
})
我的请求JSON类似于
@POST
@Path("/")
@Consumes("application/json")
@Produces(MediaType.APPLICATION_JSON)
@ValidateRequest
@ApiOperation(value = "updateDetails", nickname = "updateDetails")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = responseVO.class),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public @Valid
PortalResponseVO updateDetails( @RequestBody Object JSONObj){
...
}
我想将JSON对象映射到Java Map对象,这样我就可以使用详细信息而不必为其创建对象类。但是,由于有些字段是字符串,有些字段是嵌套映射,所以我不确定如何解析对象。我尝试使用{
name: "name1",
description: "updating details by env",
"env" : {
"env1" : {
"envName" : "envName1",
"changeDesc" : "description1"
},
"env2" : {
"envName" : "envName2",
"changeDesc" : "description2"
}
}
}
,但是每次尝试访问HashMap<String, Object> result = new ObjectMapper().readValue(JSONObj, HashMap.class);
之类的字段时,都会收到错误String name = JSONObj.get("name")
编辑
此后,我将代码更改为:
incompatible types: java.util.Map cannot be converted to java.lang.String
我已经编译了它,看起来似乎还可以,但是我想知道这能行吗?数据结构是静态的,除了名称和描述之外,是否可以强制这样投射对象?