我需要在POST请求的主体中接受JSON数据。是否可以接受POST方法主体中存在的所有输入JSON数据,而无需为JSON对象中的所有键定义getter和setter方法?
@POST
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public postData GiveData(final postData output) {
return output;
}
我的Get和Set方法::
public class TestClass {
@JsonProperty("Type")
public String getType() {
return Type;
}
public void setType(String Type) {
this.Type = Type;
}
}
如果JSON输入为
{
Type : "Test"
}
Returns 200 :
但是如果JSON输入为
{
"Type" : "Test" ,
"Random-KeY" : "Value"
}
Returns 400 : Unable to Process JSON data
我的问题是,我需要在无法期待传入的JSON密钥的地方接受数据,因此我无法为所有密钥编写get和set方法,因此,如何在POST方法的主体中接受所有JSON对象。有什么建议可以帮助我吗?
您可以用]标记您的班级>
@JsonIgnoreProperties(ignoreUnknown = true)
这将仅映射类中的现有字段,而忽略其余字段。
这可以帮助您解决问题。