Java |验证包装器内的 JSON 个对象
Java | Validating JSON objects inside wrapper
我正在使用 Java Spring 框架并收到 JSON 输入。它由数千个 MyObjects
的数组组成。如果 MyObjects
或 AnotherObjects
之一未通过约束(@NotNull
或 @Size
),我希望应用程序抛出异常。
到目前为止,应用程序在对对象执行某些操作时一旦访问 null
字段就会抛出 NullPointerException
- 而不是在其构造期间。
我的问题是:
Is there a way to check the constraints of nested JSON objects preferrably with annotation?
JSON 看起来像:
{
"myObjects": [
{
"code": "PQ",
"another_objects": [
{
"attr1": "value1",
"attr2": "value2",
"attrN": "valueN"
},
{
"attr1": "value1",
"attr2": "value2",
"attrN": "valueN"
}
]
},
{
...
}
]
}
servlet 看起来像:
@RequestMapping(value = ...)
public final void doSomething(@Valid @RequestBody MyObjectWrapper wrapper) {
// do something very time-heavy here
}
对象定义如下:
public class MyObjectWrapper {
private List<MyObject> myObjects;
public List<MyObject> getMyObjects() {
return myObjects;
}
public void setMyObjects(List<MyObjects> myObjects) {
this.myObjects = myObjects;
}
}
和 MyObject class:
public class MyObject {
@NotNull
@NotEmpty(message = ...)
List<AnotherObject> anotherObjects;
@NotNull
@Size(min = 2, max = 2, message = ...)
String code;
@JsonCreator
public MyObject(@JsonProperty("another_objects") List<AnotherObjects> anotherObjects,
@JsonProperty("code") String code) {
this.code = code;
this.anotherObjects = anotherObjects;
}
/* getters and setters */
}
AnotherObjects 类似,但仅包含字符串。
将@Valid
添加到包装器的嵌套对象
public class MyObjectWrapper {
@Valid
private List<MyObject> myObjects;
public List<MyObject> getMyObjects() {
return myObjects;
}
public void setMyObjects(List<MyObjects> myObjects) {
this.myObjects = myObjects;
}
}
我正在使用 Java Spring 框架并收到 JSON 输入。它由数千个 MyObjects
的数组组成。如果 MyObjects
或 AnotherObjects
之一未通过约束(@NotNull
或 @Size
),我希望应用程序抛出异常。
到目前为止,应用程序在对对象执行某些操作时一旦访问 null
字段就会抛出 NullPointerException
- 而不是在其构造期间。
我的问题是:
Is there a way to check the constraints of nested JSON objects preferrably with annotation?
JSON 看起来像:
{
"myObjects": [
{
"code": "PQ",
"another_objects": [
{
"attr1": "value1",
"attr2": "value2",
"attrN": "valueN"
},
{
"attr1": "value1",
"attr2": "value2",
"attrN": "valueN"
}
]
},
{
...
}
]
}
servlet 看起来像:
@RequestMapping(value = ...)
public final void doSomething(@Valid @RequestBody MyObjectWrapper wrapper) {
// do something very time-heavy here
}
对象定义如下:
public class MyObjectWrapper {
private List<MyObject> myObjects;
public List<MyObject> getMyObjects() {
return myObjects;
}
public void setMyObjects(List<MyObjects> myObjects) {
this.myObjects = myObjects;
}
}
和 MyObject class:
public class MyObject {
@NotNull
@NotEmpty(message = ...)
List<AnotherObject> anotherObjects;
@NotNull
@Size(min = 2, max = 2, message = ...)
String code;
@JsonCreator
public MyObject(@JsonProperty("another_objects") List<AnotherObjects> anotherObjects,
@JsonProperty("code") String code) {
this.code = code;
this.anotherObjects = anotherObjects;
}
/* getters and setters */
}
AnotherObjects 类似,但仅包含字符串。
将@Valid
添加到包装器的嵌套对象
public class MyObjectWrapper {
@Valid
private List<MyObject> myObjects;
public List<MyObject> getMyObjects() {
return myObjects;
}
public void setMyObjects(List<MyObjects> myObjects) {
this.myObjects = myObjects;
}
}