将 jsonObject 转换为模型对象时如何检测缺少的字段

How to detect missing field when converting jsonObject to Model Object

我正在使用网络服务发送 JsonObject。接收调用的方法具有模型对象的方法参数,用于传递相应的 JsonObject 输入。 JsonObject 已成功转换为模型对象,但我无法测试来自模型对象的一些响应。

例如:如果输入 JSON 中缺少任何必填字段,或者如果输入 JSON 中的任何字段设置为 null,那么在这两种情况下,模型对象字段即将变为 null.

我无法检测 null 是因为字段丢失还是字段被设置为 null

任何人都可以建议我一个解决方案吗?

我最后的出路是,我将接收方法参数输入作为 String 而不是我的模型对象,然后使用条件检查,检查输入字符串并相应地得到我的响应。但是我的 JSON 输入对象非常大,我希望有比这更好的方法。

它是一个 Spring 引导项目。

代码片段:

JSON Object being sent

{"orgType":null, "orgLocation": "Geneva" , //other fields}


@RequestMapping(value="/addOrganizations", method= RequestMethod.POST)
public ResponseEntity addOrganization(@RequestBody Organization organization){
// code goes here
}

如果我尝试

{"orgType":null, "orgLocation": "Geneva" , //other fields}

或:

{"orgLocation": "Geneva" , //other fields}

并在 addOrganization() 方法中使用调试器检查,然后在这两种情况下该值都是 null

型号class:

public class Organization{

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String orgType;

private String orgLocation;

// other fields and getters and setters.

}

我正在研究 Whosebug 的 links,最后通过这个 link

得到了答案

Jackson: What happens if a property is missing?

想法可能对其他人有帮助。

谢谢