Spring @RequestParam對象綁定
在Spring中,如果在方法參數列表中使用@RequestParam標注多個參數,會讓映射方法的可讀性大大降低。
如果映射請求的參數只有一兩個的話,使用@RequestParam會非常直觀,但是如果參數列表越來越長,就很容易暈菜。
解決方案
可以直接使用ParameterObject模式來處理【注:ParameterObject就是把參數組裝成對象】。
如果要傳參對數據庫操作,則參數對應數據庫中的某些字段,此時表對應的entity對象可以直接作為ParameterObject。
其他情況下則可以使用一個POJO來包裝這些參數,這個POJO本身沒有要求額外的注解,但是POJO本身必須包含和請求參數完全匹配的字段,標準的setter/getter,和一個無參的構造器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class ProductCriteria { private String query; private int offset; private int limit; ProductCriteria() { } public String getQuery() { return query; } public void setQuery(String query) { this .query = query; } // other getters/setters } @GetMapping List<Product> searchProducts(ProductCriteria productCriteria) { return productRepository.search(productCriteria); } |
在POJO中對請求參數進行校驗
雖然上面的案例已經可以正常使用,但是我們知道,使用@RequestParam注解,不僅僅只是為了綁定請求參數,一個非常重要的功能是,我們可以對綁定的參數請求驗證,比如參數是否必要,如果請求中缺少該參數,則我們的服務端可以拒絕該請求。
想為我們的POJO中的字段添加驗證規則。如果想模仿@RequestParam(required = false)的表現,可以使用@NotNull注解在對應的字段上即可。
在更多的情況下,我們一般使用@NotBlank多于@NotNull,因為@NotBlank考慮了空字符串的情況。
1
2
3
4
5
6
7
8
9
|
final class ProductCriteria { @NotBlank private String query; @Min ( 0 ) private int offset; @Min ( 1 ) private int limi; // ... } |
這里務必注意一點:
僅僅只是在對象的字段上添加驗證注解是不夠的。
一定要在controller的方法參數里誒包中,在POJO對應的參數前加上@Valid注解。該注解會讓Spring在綁定參數前執行校驗動作。
1
2
3
4
|
@GetMapping List<Product> searchProducts( @Valid ProductCriteria productCriteria) { // ... } |
@RequestParam注解的另一個非常有用的功能就是設置參數的默認值。
如果我們使用POJO的方式來綁定參數,只需要在定義參數的時候設置好字段的默認值就行了。如果請求中沒有該參數,Spring不會把參數的默認值覆蓋為null的。
SpringMvc參數綁定自定義對象
springmvc我們經常在寫controller一般都接受兩種方式,一種是form提交,一種是json提交,下面就來介紹如何在這兩種方式中將提交的數據自動綁定到自定義對象中。
json提交
這個比較簡單,在網上搜一下一大把,這里就簡單放一段代碼:
1
2
3
4
|
@RequestMapping ( "/testjson" ) public String testjson( @RequestBody User user){ return "ok" ; } |
form提交
這個是比較頭疼的,一般form有很多的參數,我們可以像下面這樣寫:
1
2
3
4
|
@RequestMapping ( "/test" ) public String testParam( @RequestParam (name = "name" ) String name, @RequestParam (name = "sex" ) String sex) { return name + sex; } |
但是如果我改成下面這樣會怎么樣?然后用form提交參數 name=zack & sex=boy
1
2
3
4
|
@RequestMapping ( "/test" ) public String test( @RequestParam (name = "user" ) User user) { return user.getName(); } |
結果是報錯:
{
"timestamp": "2018-05-29T11:58:37.450+0000",
"status": 400,
"error": "Bad Request",
"message": "Required User parameter 'user' is not present",
"path": "/test1"
}
我的參數里確實是沒有user這個,其實我的原本目的是想讓spring把我傳遞的name和sex熟悉拼裝好生成一個user對象,因為剛好user對象就有這2個屬性,spring可沒有智能。那該怎么辦?
這個時候引入WebDataBinder, 在你的controller里加上下面的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(User. class , new UserFormatEditor()); } public static class UserFormatEditor extends PropertiesEditor { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(JSONObject.parseObject(text, User. class )); } @Override public String getAsText() { return getValue().toString(); } } |
然后在請求時將參數改為 user = {"name":"zack","sex":"boy"} ,之后就成功的獲取User對象,WebDataBinder幫我們告訴了spring,如果遇到了一個字符串參數要被包裝成User.class,用我們自定義的UserFormatEditor就行。
小結一下
作為規范而言,form提交的方式本身就需要我們一個一個屬性的接收,而不能用一個對象統一接收,如果你想用一個對象統一接收,請采用json的方式提交。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/lcx20190724xxz/p/11237164.html