简体   繁体   中英

How to make input attributes required in post body request?

Here is my example: Java:

 @JsonProperty("id")
private String id;
@JsonProperty(value = "name", required = true)
private String deviceName;

I made the name as a required field. In request how to make it as required field. I should send the name value from request.

But when I enter this:

{ "id": "abc123",}

It should send error response back.

Please help me.

Jacksons JsonProperty Annotation is not used for Validation. see: Jackson @JsonProperty(required=true) doesn't throw an exception . But you can use Bean Validation , eg:

class Device {

    @JsonProperty("id")
    private String id;

    @NotEmpty
    @JsonProperty(value = "name")
    private String deviceName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM