Reputation: 36299
I have the following:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/some/path")
void createSomething(@ApiParam(required = true) @Valid final User user);
And User
is something like
class User {
@Valid
@NotNull
@NotBlank
final String name;
...
}
I do get back 400 if I do curl -X POST -H "Content-Type: application/json" /some/path -d '{"foo":"bar"}'
.
However, if I do not send any thing, then I do not get 400, i.e curl -X POST -H "Content-Type: application/json" /some/path
does not result in 400 (and later results in 500 cause the object user
is now null.
What am I doing wrong?
Upvotes: 3
Views: 4492
Reputation: 2701
Swagger annotations do not perform any server-side validation of the input - they are only used to generate the swagger doc. Then you have to somehow create an user interface from the generated Swagger doc (for example by using Springfox swagger-ui if using Spring Boot https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui). And finally, if the UI is generated correctly, the UI would perform the validation.
Since in the examples you were sending the requests using Curl, the swagger annotations do nothing (because you aren't sending the requests through Swagger UI) and thus you can send null value. In order to perform server-side validation you have to use other annotations.
Upvotes: 3