pat*_*ide 5 java spring spring-mvc
例如,这是一个 Get 请求:
得到:/search?product_category=1&user_name=obama
我想定义一个SearchRequest来接受查询字符串,所以我可以使用 JSR 303 bean 验证注释来验证参数,如下所示:
public class SearchRequest {
@NotEmpty(message="product category is empty")
private int productCategory;
@NotEmpty(message="user name is empty")
private int userName;
}
Run Code Online (Sandbox Code Playgroud)
那么,是否有类似@JsonPropertyjackson 的东西将下划线样式转换为骆驼样式?
你只有两个选择;
第一的。让您的 SearchRequest pojo 带有用于验证的注释值,但让控制器 POST 方法接收 pojo 作为 JSON/XML 格式的请求正文。
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) {
...
}
Run Code Online (Sandbox Code Playgroud)
第二。在 Controller 方法签名本身中进行验证,消除了 pojo 中的验证,但如果需要,您仍然可以使用 pojo。
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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1569 次 |
| 最近记录: |