问题描述:

I a trying to parse an Object into a JSON String and validate the JSON before sending. Using Spring @Valid tag to make sure JSON is valid based on the parameters specified in the Object definition.

However, I see that there is no check on the repetition of fields in the JSON. Is there a tag to control the repetition of fields?

Object:

public class Test {

@Size(min=2, max=30) @NotNull

private String name;

@NotEmpty @Email

private String email;

@NotNull @Min(13) @Max(110)

private Integer age;

@Size(min=10)

private String phone;

@NotNull

private Gender gender;

}

Call:

public void putTest(..., @RequestBody @Valid Test test,...)

Unfortunately it accepts a Test object of the following format:

{Name: "Test", Email: "[email protected]om", age: 20, **Gender: "M", Gender:"F"**}

网友答案:

if your JSON post correct, you have a problem name of the fields

See:

{Name: "Test", Email: "[email protected]", age: 20, **Gender: "M", Gender:"F"**}

first letters of the properties is uppercase.

when solution for this, map properties same

import com.fasterxml.jackson.annotation.JsonProperty;


public class Test {

    @Size(min=2, max=30)  @NotNull
    @JsonProperty("Name")
    private String name;

    @NotEmpty @Email
    @JsonProperty("Email")
    private String email;

    @NotNull @Min(13) @Max(110)
    @JsonProperty("Age")
    private Integer age;

    @Size(min=10)
    @JsonProperty("Phone")
    private String phone;

    @NotNull
    @JsonProperty("Gender")
    private Gender gender;
}
相关阅读:
Top