是否可以在没有getter和setter方法的情况下在POST请求的主体中接受JSON对象?

问题描述 投票:1回答:2

我需要在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对象。有什么建议可以帮助我吗?

java json rest jax-rs dropwizard
2个回答
0
投票

您可以用]标记您的班级>

@JsonIgnoreProperties(ignoreUnknown = true)

这将仅映射类中的现有字段,而忽略其余字段。


0
投票

这可以帮助您解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.