For example, here's a Get request:
get: /search?product_category=1&user_name=obama
I want to define a SearchRequest
to accept the query string, so I can use JSR 303 bean validation annotations to validate parameters, like below:
public class SearchRequest {
@NotEmpty(message="product category is empty")
private int productCategory;
@NotEmpty(message="user name is empty")
private int userName;
}
So, is there something like @JsonProperty
in jackson to convert underscore style to camel style?
You only have two options;
First. Have your SearchRequest pojo with annotated values for validation but have a controller POST method receive the pojo as request body as a JSON/XML format.
public class SearchRequest {
@NotEmpty(message="product category is empty")
private int productCategory;
@NotEmpty(message="user name is empty")
private int userName;
}
public String search(@RequestBody @Valid SearchRequest search) {
...
}
Second. Have validations in the Controller method signature itself eliminating validations in the pojo but you can still use the pojo if you want.
public class SearchRequest {
private int productCategory;
private int userName;
}
public String search(@RequestParam("product_category") @NotEmpty(message="product category is empty") String productCategory, @RequestParam("user_name") @NotEmpty(message="user name is empty") String username) {
... // Code to set the productCategory and username to SearchRequest pojo.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With