alexlipa
alexlipa

Reputation: 1271

Dropwizard: make JSON integer parameter mandatory

I am developing a service using Dropwizard. In a POST request I receive a JSON and I want to throw an exception if the format of the request is not valid, in particular if some fields are missing. By default, as in the documentation, the Hibernate Validator is being used. This is the code:

public class ExtranetRequest {

    @NotEmpty(message = "extranet_request_id should not be empty")
    public String extranet_request_id;

    @NotNull
    public int hotel_id;

    public List<ShopPattern> shop_patterns;
}

Everything works as expected for the field extranet_request_id (an exception is thrown if the field is not present in the JSON). However the request no error is raised if the field hotel_id is missing. I also tried the annotations @NotEmpty, @NotBlank, @Min(0), but none of them worked.

Upvotes: 0

Views: 547

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10519

Did you try to make the hotel_id field an Integer?

An int cannot be null so it will be 0 by default which is OK for @NotNull or @Min(0) (it's checking that the number is higher or equal).

@NotEmpty or @NotBlank should make Hibernate Validator throw an exception though.

Upvotes: 1

Related Questions